繁体   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