簡體   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