2

I am trying to access an SQLite 3 database programmatically using the code below:

private void button1_Click(object sender, EventArgs e)
{
    try
    {

        string connectionPath = @"Data Source=C:\temp\sms.db";

        SQLiteConnection connection = new SQLiteConnection(connectionPath);

        SQLiteCommand command = connection.CreateCommand();
        connection.Open();
        string query = "SELECT * FROM Message";
        command.CommandText = query;
        command.ExecuteNonQuery();

        SQLiteDataAdapter dataAdaptor = new SQLiteDataAdapter(command);
        DataSet dataset = new DataSet();
        dataAdaptor.Fill(dataset, "messages");
        dataGridView1.DataSource = dataset.Tables["messages"];
        connection.Close();

    }

    catch
    {
        MessageBox.Show("error", "error", MessageBoxButtons.OK);
    }
}

However whenever I press the button this code is assigned to, it doesn't catch on the Try Catch and display my message box, it freezes for a few seconds and debugs into the Program.cs file at the line shown below

    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new frmMainWindow()); **<----This Line**
    }
}

At that line it then displays this error to me

Could not load file or assembly 'System.Data.SQLite, Version=1.0.76.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139' or one of its dependencies.
An attempt was made to load a program with an incorrect format.

How can I overcome this error so I can query the database?

EDIT

My development OS is Win 7 x64 Prof
I have installed:
Setups for 64-bit Windows (.NET Framework 4.0)
Setups for 32-bit Windows (.NET Framework 4.0)
from heresystem.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki

1
  • 2
    Sounds like a 32-/64-bit mismatch. Is your development OS 64-bit?? If so: did you install both versions of SQLite? The tricky part is: since Visual Studio is 32-bit, you need the 32-bit version when running inside VS. If you run outside VS, you get the 64-bit version by default (unless you explicitly compile for "x86" only) Commented Apr 1, 2012 at 10:11

2 Answers 2

2

What dll are you using? if you are in x64 system you have to use x64 dll of SQLite

Sign up to request clarification or add additional context in comments.

Comments

2

Turned out Visual Studio was incorrectly referencing a different version of System.Data.SQLite.dll for some reason

I removed the old reference, and copied the new System.Data.SQLite.dll files to the location:

C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0\Profile\Client

Then added reference to it using Visual Studio again and it now works

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.