Creating a Local DB for a WPF Application

advertisements

I have an application and I want to create a DB in the same folder as the application. I looked into SQLite for .NET 4.0 everything looked fine untill I got an exception "Could not load file or assembly 'System.Data.SQLite, Version=1.0.88.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139' or one of its dependencies. An attempt was made to load a program with an incorrect format." The SQLite works with a console .NET application. I dont want to use SQL Server at all. Is there an alternative other than XML files?


SQLite is a great choice for the scenario you describe. Giving up on SQLite because you could not include it correctly seems like the wrong conclusion. Reference it through NuGet and you are fine. The library needs SQLite.Interop.dll which you might not have included correctly. You might also have included something like the x64 version when you are running under x86 (see this for details). Use it through NuGet and you should be OK.

Here is the quick setup I got working in under 5 minutes:

  1. Created console application (VS Express 2012 for Desktop, .NET v4, Any CPU, Debug)
  2. Installed via NuGet
  3. Used the following code
  4. Works like a charm!

Code sample:

string dataSource = "SQLiteDemo.db";
SQLiteConnection connection = new SQLiteConnection();
connection.ConnectionString = "Data Source=" + dataSource;
connection.Open();
SQLiteCommand command = new SQLiteCommand(connection);
command.CommandText = "CREATE TABLE IF NOT EXISTS beispiel ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name VARCHAR(100) NOT NULL);";
command.ExecuteNonQuery();
command.CommandText = "INSERT INTO beispiel (id, name) VALUES(NULL, 'Test-Datensatz!')";
command.ExecuteNonQuery();
command.Dispose();
connection.Dispose();