简体   繁体   English

使用 C# FormApp 和 ADO.NET 读取 db 文件

[英]Read a db file with C# FormApp and ADO.NET

I'm trying to read a simple sqlite db file (which I create with DB Browser) with c# form application ADO.NET.我正在尝试使用 C# 表单应用程序 ADO.NET 读取一个简单的 sqlite db 文件(我使用 DB Browser 创建)。

    using System.Data.SqlClient;
    private void button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = ConfigurationManager.ConnectionStrings["DefaultC"].ToString();
        con.Open();
        MessageBox.Show("Connection Seccessfull!");
    } 

In App.config:在 App.config 中:

    <connectionStrings>
      <add name="DefaultC" connectionString="ExamDB.db;Database=ExamDB;Trusted_Connection=true;MultipleActiveResultSets=true" />
    </connectionStrings>

Getting an error for this request.收到此请求的错误。 Note that I save the db file in the bin/debug folder and the main root as well.请注意,我将 db 文件保存在 bin/debug 文件夹和主根目录中。

please your help请你的帮助

You use sqlclient, but this is an db file, so you should use Microsoft.Data.SQLite package.您使用 sqlclient,但这是一个 db 文件,因此您应该使用Microsoft.Data.SQLite包。

Here is the modified code:这是修改后的代码:

SqliteConnection con = new SqliteConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["DefaultC"].ToString();
con.Open();
MessageBox.Show("Connection Seccessfull!");

And the modified ConnectionString:以及修改后的 ConnectionString:

<connectionStrings>
    <add name="DefaultC" connectionString="Data Source=ExamDB.db" />
</connectionStrings>

Here is a funddamental issue:这里有一个基本问题:

sqlite db file sqlite 数据库文件

Yeah, get that, but then WHY...是的,明白了,但是为什么......

using System.Data.SqlClient;使用 System.Data.SqlClient;

That is the client (as per documentation) for MS SQL Server.那是 MS SQL Server 的客户端(根据文档)。 OBVIOUSLY it will not work - different technology stack.显然它不起作用 - 不同的技术堆栈。

Use the Microsoft.Data.SqlLite package available at https://www.nuget.org/packages/Microsoft.Data.Sqlite/5.0.11?_src=template使用https://www.nuget.org/packages/Microsoft.Data.Sqlite/5.0.11?_src=template提供的 Microsoft.Data.SqlLite 包

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM