c# - 使用SqlConnection连接到SQLite时出错,为什么?

标签 c# sql sqlite

我正在尝试连接到本地数据库(计算机中的“.db”文件),但在尝试打开连接时出现错误。

我正在使用Sharp Develop(我无法使用Visual Studio的连接向导来获取con.String)。

    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)

发生了什么事?

最佳答案

您正在使用 SqlConnection 作为 SQLite 数据库文件 (.db)。

您必须下载 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 类似,即绑定(bind) DataSet。

我没有使用 SharpDevelop,而是使用了 Visual Studio,希望它能起作用

关于c# - 使用SqlConnection连接到SQLite时出错,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44902437/

相关文章:

c# - 方法的不同

c# - XML 列表项属性

sql - SQLite:按2列选择最后一行

java - 如何使用比较器对集合中的多个字段进行排序?

android - 使用 SQLCipher 加密后无法压缩 Android SQLite 数据库

c# - 如何在 ListView 中将我们的样式赋予数据分页

c# - NHibernate - 对于分离的父实体,级联合并到子实体失败

mysql - SQL 查询解释的结果是否取决于数据库的大小?

SQLite:多个聚合列

Android SQL 查询无法识别第一个条目