我正在尝试连接到本地数据库(计算机中的“.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/