我在连接到 .sdf(sql 紧凑版)数据库时遇到了巨大的麻烦。我可以最初连接以提取行以验证用户名/密码,但是当我尝试通过 SqlCeConnection/SqlCeCommand 命令或尝试添加项目 SqlClient/SqlCommand 连接向数据库添加项目时,我没有运气。我得到:
A network-related or instance-specific error occurred while establishing
a connection to SQL Server. The server was not found or was not accessible.
Verify that the instance name is correct and that SQL Server is configured to
allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error
Locating Server/Instance Specified)
使用时:
private void button1_Click_1(object sender, EventArgs e)
{
System.Data.SqlClient.SqlConnection sqlConnection1 =
new System.Data.SqlClient.SqlConnection("Data Source=C:\\Users\\Tim\\Documents\\Visual Studio 2010\\Projects\\Dispatch POS\\Dispatch POS\\GhsDB.sdf");
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = "INSERT [Employee Table] (SSN, FirstName) VALUES ('555-23-4322', 'Tim')";
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
cmd.ExecuteNonQuery();
sqlConnection1.Close();
}
或者当我尝试时:
System.Data.SqlClient.SqlConnection sqlConnection1 =
new System.Data.SqlClient.SqlConnection("Data Source=C:\\Users\\Tim\\Documents\\Visual Studio 2010\\Projects\\Dispatch POS\\Dispatch POS\\GhsDB.sdf");
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = "INSERT [Employee Table] (SSN, FirstName) VALUES ('555-23-4322', 'Tim')";
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
cmd.ExecuteNonQuery();
sqlConnection1.Close();
什么也没发生,它似乎一直有效,直到我检查 .sdf 文件并看到没有添加任何内容。我没有安装 SQL Server CE,但我安装了 2008 R2(我正在处理别人的项目)。
现在我并不担心安全漏洞,我只是想成功添加一条记录。任何帮助将不胜感激,因为我花了大约三四个小时来处理一些我认为需要大约 20 分钟的事情。