繁体   English   中英

连接到SQL Server CE数据库

[英]Connect to SQL Server CE database

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

System.Data.dll中发生了类型为'System.Data.SqlClient.SqlException'的未处理的异常

附加信息:建立与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();

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM