2017-07-04 82 views
0

我試圖連接到本地數據庫(我的計算機中的「.db」文件),但當我嘗試打開連接時出現錯誤。我正在使用Sharp Develop(我不能使用Visual Studio的連接嚮導來獲取連接字符串)。使用SqlConnection連接到SQLite時出錯,爲什麼?

String connectionString = @"Data Source=C:\Users\Adl\Documents\BBDD_Test.db"; 
    var c = new SqlConnection(connectionString); 
    c.Open(); 

錯誤:

System.Data.SqlClient.SqlException: 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)

發生了什麼事?

+0

什麼是你的數據庫格式? SQLite的? – Pikoh

+1

數據庫格式是SQLite 3 – Ralk

+2

然後你不能使用'SqlConnection',這是Sql Server。你需要下載[System.Data.Sqlite](https://system.data.sqlite.org/index.html/doc/trunk/www/index.wiki)並使用它來代替 – Pikoh

回答

1

您正在使用SQLite數據庫文件(.db)的SqlConnection。

您將不得不下載SQLite的ADO.NET提供程序或將System.Data.SQLite.dll添加到您的解決方案中。現在你使用某些SQLite類。 SQLiteConnection,SQLiteCommand和SQLiteDataAdapter

String connectionString = @"Data Source=D:\Users\wkhan2\Downloads\chinook\chinook.db"; 

     System.Data.SQLite.SQLiteConnection conn = new System.Data.SQLite.SQLiteConnection(connectionString); 
     System.Data.SQLite.SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand("select * from table"); 
     cmd.Connection = conn; 

     conn.Open(); 
     cmd.ExecuteScalar(); 
     System.Data.SQLite.SQLiteDataAdapter da = new System.Data.SQLite.SQLiteDataAdapter(cmd); 
     System.Data.DataSet ds = new System.Data.DataSet(); 

     da.Fill(ds); 

其餘與ado.net類似,即綁定DataSet。

嗯,我沒有使用SharpDevelop的,而使用Visual Studio中,希望工程