Abhilash JA Abhilash JA - 27 days ago 6
C# Question

C# - Access SQLite dabase for WPF application

I have create Set up file for WPF application and the database is SQLite DB. After the installation the SQLite DB is located this path in C folder,

C:\Program Files (x86)\myCompany\myScanApp.

enter image description here

Then I have written connection string like this way for establishing a connection to SQLite DB,

static SQLiteConnection dbConnection = new SQLiteConnection(@"Data Source=C:\Program Files (x86)\myCompany\myScanApp\test.s3db;");


Then I have created a Setup file again and then run this application on another PC. But, it is failing to run in the PC. How can I access DB in application?

Answer
using(SQLiteConnection conn= new SQLiteConnection(@"Data Source=C:\Program Files (x86)\myCompany\myScanApp\test.s3db;"))
{
    conn.Open();

    SQLiteCommand command = new SQLiteCommand("Select * from yourTable", conn);
    SQLiteDataReader reader = command.ExecuteReader();

    while (reader.Read())
       Console.WriteLine(reader["YourColumn"]);


    reader.Close();
}

Something like this should work. Here whole article Getting started with SQLite in C#