c# - 通过 SqlConnection/SqlCeConnection 连接到 .sdf 数据库时出现问题

标签 c# sql sql-server

我在连接到 .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 分钟的事情。

最佳答案

可能您现在已经找到了解决方案,但是如果您使用 sdf 文件,您应该在程序集中添加对 System.Data.SqlServerCe 的引用,然后添加类似以下内容:

SqlCeConnection sqlConnection1= new SqlCeConnection();
sqlConnection1.ConnectionString = "Data Source = C:\\Users\\Tim\\Documents\\Visual Studio 2010\\Projects\\Dispatch POS\\Dispatch POS\\GhsDB.sdf";

System.Data.SqlServerCe.SqlCeCommand cmd = System.Data.SqlServerCe.SqlCeCommand;
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();

关于c# - 通过 SqlConnection/SqlCeConnection 连接到 .sdf 数据库时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4363042/

相关文章:

c# - Linq to XML 搜索

c# - ComboBox SelectedItem 绑定(bind)不更新

c# - 如何只获取最新的发票编号

sql - 正在寻找编译器,有什么想法吗?

sql-server - 在 SQL Server 中,如何以类似于 Oracle 的 "SELECT FOR UPDATE WAIT"的方式锁定单行?

sql-server - 防止 Microsoft Access 锁定 MSSQL View Serverside?

c# - Web.config 的 system.web/Pages/Namespaces 标签有什么意义?

php - 从 MySQL 表获取排名到 TextView

java - 如何在 SQL 语句中使用生成的标识 (NetBeans JavaDB)

PHP/MySQL 中的 SQL Server 'FOR XML PATH'