我实际上正在使用Visual C#Express 2010开发一个Windows窗体应用程序,该应用程序将使用SQL Server 2008 Express DB 中的(读/写)数据
我已经用SQL Server Management Studio(2008 Express)创建了我的数据库,我知道该实例名为ATLELAG786576SQLEXPRESS
我的数据库被称为"测试">
在SQL Server Management Studio(2008 Express)中查看我的数据库"TEST"属性:在"文件"下,我是DB 的所有者(ATLEbneveux
)
查看Security,Logins,Mylogin(ATLE\bneveux)
- 我的默认数据库是"TEST">
- 服务器角色为"public"+"sysadmin">
- 用户映射数据库'TEST'用户'dbo'默认架构'dbo]
在我的C#应用程序中
app.config:
<?xml version="1.0" encoding="utf-8" ?> <configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="connectionStringTestDb"
connectionString="Data Source=ATLELAG786576SQLEXPRESS;Initial Catalog=D:Microsoft SQL ServerMSSQL10.SQLEXPRESSMSSQLDATATEST.mdf;Integrated Security=True;Connect Timeout=30;User Instance=False"
providerName="System.Data.SqlClient" />
</connectionStrings> </configuration>
dbConnection.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace SQLServerConnectionDemo
{
class dbConnection
{
public static SqlConnection newCon;
public static string connectionStringTestDb = ConfigurationManager.ConnectionStrings["connectionStringTestDb"].ConnectionString;
public static SqlConnection GetConnection()
{
newCon = new SqlConnection(connectionStringTestDb);
return newCon;
}
}
}
dbAccess.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace SQLServerConnectionDemo
{
class dbAccess
{
SqlConnection conn;
public dbAccess()
{
conn = dbConnection.GetConnection();
}
//Method insert new in tblEmployees
public void addEmployee(string Id, string Name, string Email)
{
if (conn.State.ToString() == "Closed")
{
conn.Open();
}
SqlCommand newCmd = conn.CreateCommand();
newCmd.Connection = conn;
newCmd.CommandType = CommandType.Text;
newCmd.CommandText = "INSERT INTO tblEmployees VALUES ('"+ Id +"','"+ Name +"','"+ Email +"')";
newCmd.ExecuteNonQuery();
}
}
}
表格形式EmployeeAdd.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace SQLServerConnectionDemo
{
public partial class formEmployeeAdd : Form
{
dbAccess access = new dbAccess();
public formEmployeeAdd()
{
InitializeComponent();
}
private void btnInsert_Click(object sender, EventArgs e)
{
access.addEmployee(txtId.Text, txtName.Text, txtEmail.Text);
MessageBox.Show("Data successfully added");
}
}
}
这里是我在尝试运行这个过程时总是收到的错误消息:
System.Data.SqlClient.SqlException(0x80131904):无法打开登录请求的数据库"D:\Microsoft SQL Server\MSSQL10.SQLEXPRESS \MSSQL\Data\TEST.mdf"。登录失败。用户"ATLE\bneveux"登录失败。
请注意,我从未真正能够在Visual C#2010学习版中添加我的数据源,因此我可以从VS管理数据库,我总是收到以下错误消息:
无法打开物理文件"D:\Microsoft SQL Server\MSSQL10.SQLEXPRESS \MSSQL\DATA\TEST.mdf"。操作系统错误32:"32(进程使用进程)"。尝试为文件D:\Microsoft SQL Server\MSSQL10.SQLEXPRESS \MSSQL\DATA\TEST.mdf附加自动命名数据库失败。存在具有相同名称的数据库,或者指定的文件无法打开,或者它位于UNC共享上。
尝试更换
Initial Catalog=D:Microsoft SQL ServerMSSQL10.SQLEXPRESSMSSQLDATATEST.mdf
只需
Initial Catalog=TEST