簡體   English   中英

如何使用C#訪問SQLite?

[英]How can I access SQLite with C#?

我正在嘗試使用C#/ ASP.NET以編程方式連接到我的Sqlite數據庫:

string requete_sql = "SELECT * FROM USERS";
connStr = @"Data Source=C:\LocalFolder\FooBar.db;";
using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connStr)) {
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(requete_sql,conn);
conn.Open();
cmd.ExecuteNonQuery();
}

但是異常上升(在conn.Open()行上)告訴:

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)

這很奇怪,因為我復制了Web.config文件中找到的確切連接字符串。

我怎樣才能避免這種異常?

PS:我的目標是在沒有web.config文件的情況下以編程方式連接到數據庫。

謝謝,

問候。

C#中的SQLite(引用中需要System.Data.SQLite

// Required references, after installing SQLite via Nuget
using System.Data.SQLite;
using System.Data.Common;

// Example usage in code...
SQLiteConnection db = new SQLiteConnection("Data Source=C:\LocalFolder\FooBar.db;FailIfMissing=True;");
db.Open();
using (SQLiteCommand comm=db.CreateCommand()) {
  comm.CommandText = requete_sql;
  IDataReader dr=comm.ExecuteReader();
  while (dr.Read())
  {
    //...
  }
}

您無法使用SQLProvider類連接到sqlite db。 它們用於sql server。 您需要使用SQLite提供程序類。

有關MSDN雜志的文章就是這樣:

http://msdn.microsoft.com/en-us/magazine/ff898405.aspx

  1. http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki下載相應的發行版
  2. 在項目中引用System.Data.SQLite.DLL (這為您提供了SQLiteConnection類)
  3. 與連接

     SQLiteConnection connection = new SQLiteConnection(@"DbLinqProvider=Sqlite;Data Source=Database.s3db"); Main main = new Main(connection); 

有關詳細信息,請參閱https://code.google.com/p/dblinq2007/wiki/Installation#To_use_DbLinq

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM