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?
-
1This question has some good guidance for the same scenarioJonesopolis– Jonesopolis2013-09-25 13:35:46 +00:00Commented Sep 25, 2013 at 13:35
-
You can try many things cleaning and rebuilding the solution , restarting the IDE , sometimes some process uses that specific DLL so ending that process. It's simply some build issue so no need to migrate to something else.Rameez Ahmed Sayad– Rameez Ahmed Sayad2013-09-25 13:43:13 +00:00Commented Sep 25, 2013 at 13:43
2 Answers
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:
- Created console application (VS Express 2012 for Desktop, .NET v4, Any CPU, Debug)
- Installed via NuGet
- Used the following code
- 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();
10 Comments
I found one more solution to this issue. The problem was with the platform (x86/x64). I got the source code of Sqllite v1.0.88 and opened the VS 2010 solution file. I noticed that the target was mixed platforms by default. I changed it to any CPU and built the System.Data.SQLite.2010 project. I referenced this dll in my project and I did not have an issue with x86/x64 platform. One dll worked for both.