Home > Software engineering >  Read a db file with C# FormApp and ADO.NET
Read a db file with C# FormApp and ADO.NET

Time:11-07

I'm trying to read a simple sqlite db file (which I create with DB Browser) with c# form application ADO.NET.

    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:

    <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.

please your help

CodePudding user response:

You use sqlclient, but this is an db file, so you should use Microsoft.Data.SQLite package.

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:

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

CodePudding user response:

Here is a funddamental issue:

sqlite db file

Yeah, get that, but then WHY...

using System.Data.SqlClient;

That is the client (as per documentation) for 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

  • Related