Search code examples
c#sqlitewindows-ce

Opening a connection in SQLite


I am trying to use SQLite in Windows ce 5.1.17. I create a SQLite database using SQLite Administrator in debug folder of my project. Then I add System.Data.SQLite.dll and SQLite.Interop.065.dll to my project. I am trying the following code to connect to database.

SQLiteConnection myConnection = null;
myConnection = new SQLiteConnection(@"Data Source=" + System.IO.Path.Combine(System.IO.Path. GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase), Stock.s3db)+ "; Version=3; Cache Size =100");
                myConnection.Open();

                SQLiteCommand cmd = new SQLiteCommand();
                cmd.CommandText = "select * from shops;";
                cmd.CommandType = CommandType.Text;
                cmd.Connection = myConnection;
                cmd.ExecuteReader();// I get exception no such table: shops

Also when I check myConnection database property it shows 'Database = "main"'. I do not know why database name is 'main', but in myConnection I set datasource to Stock.s3db.


Solution

  • Use the connection string builder to ensure you get a correctly formatted string

    SQLiteConnectionStringBuilder builder = new SQLiteConnectionStringBuilder();
    builder.FailIfMissing = true;
    builder.DataSource = "Insert the fully qualified path to your sqlite db";
    SQLiteConnection connection = new SQLiteConnection(builder.ConnectionString);
    connection.Open();