c# - 无法创建与 sqlite 数据库的连接

标签 c# sqlite connection-string

我编写了一个代码,可以在执行软件的文件夹中自动创建数据库。数据库创建成功,但是当我为连接创建对象时,显示此异常:

System.Data.SqlClient.SqlException (0x80131904): There was a network error or specific instance while trying to establish 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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) ---> System.ComponentModel.Win32Exception (0x80004005): Can not find the file specified

在这一行:

con.Open();

但我认为问题在于这个连接声明:

static SqlConnection con = new SqlConnection(
        "Server=localhost;Database=AppDB.sqlite;");

我做错了什么?

更新详情:

 SqlCommand command = new SqlCommand(@"INSERT INTO Team (name,code, shortName, squadMarketValue, 
                crestUrl, link_self, link_fixtures, link_players, caption) VALUES (@name,
                @code,@shortName,@squadMarketValue,@crestUrl,@link_self,@link_fixtures,
                @link_players,@caption)", con);

如何看到我正在使用参数化器查询,以及如果我使用此对象:

static SQLiteConnection m_dbConnection = new SQLiteConnection("Data Source=SoccerForecast.sqlite;Version=3;");

con 变量用红色下划线表示,编译器告诉我:

it is not possible to convert SqliteConnection in SqlConnection

最佳答案

您的连接错误。您没有在问题中指定要尝试连接的数据库类型。当您使用 sql 连接对象时,提到了 SQLite。 sql连接对象用于连接到ms sql server数据库。如果您确实想要连接到 MS SQL Server 数据库,则需要更改连接字符串以提供用户名/密码或 Trusted_Connection=True 来使用您的 Windows 帐户。

但是,如果您尝试连接的数据库是 SQLite DB,则必须使用不同的连接对象。 Thisthis SO question有关于可用库的答案和示例,这些示例将解释如何创建与 SQLite 数据库的连接。

网站connectionstring.com当您需要生成连接字符串时,这也是一个很好的资源。

关于c# - 无法创建与 sqlite 数据库的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31221339/

相关文章:

c# - emguCV-快速计算二进制图像中带孔的对象的面积

ios - 备份和接收核心数据 SQlite3 数据库

mysql - 在不同于 3306 的端口上配置 webmatrix3 mysql 连接 UI

c# - 配置字符串的 Winforms 连接属性对话框

c# - OLEDB 连接字符串中的 IMEX 是什么?

c# - C# 和 .NET 中的 libcurl

c# - 从 c++11 time_t 创建一个可转换为 .net DateTime 的字符串

c# - 如何使用 Entity Framework 关联 "fixup"?

python - sqlite3、Python中 "="和 "match"之间的区别?

java - 我的 RecyclerView 中的 SQLite 数据没有被删除