2012-08-28 108 views
0

以下代碼在執行過程中出現錯誤。數據庫連接出錯

string connectionString = "Data Source=D:\\Base.sdf;Persist Security Info=False"; 
SqlConnection sqlConnection = new SqlConnection(connectionString)) 
sqlConnection.Open(); 

的錯誤是:

A network-related or instance-specific error occured 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 not configured to allow remote connections. (provider:SQL network Interfaces, error: 26 - Error locating Server/Instance Specified)

我試過SqlCeConnection代替SqlConnection但是,編譯器無法找到與一流的圖書館。

請幫忙解決這個問題。

  • 的操作系統:Windows 7
  • 工具:微軟的Visual Studio 2010
  • 語言:C#
+4

'.sdf'是** **肯定一個SQL Server **精簡版本**(CE)數據庫 - 因此您需要確保安裝必要的程序集才能使SqlCeConnection工作。這將** **不適用於'SqlConnection'! –

+0

我需要安裝什麼組件? – Nurlan

+1

你需要SQL Server CE運行庫的東西 - 查看[Everything SQL Server Compact](http://erikej.blogspot.ch/) - 這個網站有大量的解釋,教程,how-tos等,並且肯定也會有有關需要安裝的信息以及從哪裏獲取信息 –

回答

5

你的數據庫是SQL Server精簡一個版本,你必須使用:

SqlCeConnection sqlConnection = new SqlCeConnection(connectionString); 

從這裏下載庫文件Microsoft SQL Server Compact 4.0

  1. System.Data.SqlServerCe.dll引用添加到您的項目
  2. 添加此使用指令使用System.Data.SqlServerCe;
  3. 使用SqlCeConnection而不是SqlConnection