【问题标题】:Connect to SQL Server CE database连接到 SQL Server CE 数据库
【发布时间】:2012-09-06 12:42:27
【问题描述】:

我无法连接到 .sdf(SQL Server Compact 版本)数据库。当我在打开数据库中调试我的项目时出现此错误:

“System.Data.SqlClient.SqlException”类型的未处理异常 发生在 System.Data.dll 中

附加信息:网络相关或特定于实例的错误 在建立与 SQL Server 的连接时发生。服务器是 未找到或无法访问。验证实例名称是否为 正确,并且 SQL Server 配置为允许远程连接。 (提供者:SQL 网络接口,错误:26 - 错误定位 服务器/实例指定)

我没有运气。我得到这个代码:

Qconnection.ConnectionString = "Data Source=C:\\Users\\Admin\\Desktop\\New folder\\WindowsFormsApplication2\\WindowsFormsApplication2\\Database1.sdf";
//connection.ConnectionString = " Data Source=C:\\Users\\Admin\\Desktop\\WindowsFormsApplication2\\WindowsFormsApplication2\\Database1.sdf";
Qcommand.Connection = Qconnection;
Qconnection.Open();

for (int i = 0; i < orderColection.counter1; i++)
{
   string commandText = "Insert into order values(@RID,@amount,@type,@date)";
   Qcommand.CommandText = commandText;
   Qcommand.CommandType = CommandType.Text;

   Qcommand.Parameters.AddWithValue("@RID", orderColection.list[i].rep_id);
   Qcommand.Parameters.AddWithValue("@amount", orderColection.list[i].amount);
   Qcommand.Parameters.AddWithValue("@type", orderColection.list[i].type);
   Qcommand.Parameters.AddWithValue("@date", orderColection.list[i].date );
   Qcommand.ExecuteNonQuery();
}
Qconnection.Close();

【问题讨论】:

    标签: c# sql-server-ce


    【解决方案1】:

    您必须使用System.Data.SqlServerCe 提供程序 API 来连接 SQL Server Compact 数据源。 (添加System.Data.SqlServerCe.Dll的引用)。

    using(SqlCeConnection cn = new SqlCeConnection(@"Data Source=C:\path\sample.sdf"))
    {
     //
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-08
      相关资源
      最近更新 更多