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?
-
1[This question](http://stackoverflow.com/questions/4839342/sql-server-compact-4-0-vs-sqlite) has some good guidance for the same scenario – Jonesopolis Sep 25 '13 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 Sep 25 '13 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();
-
I tried referencing it through NuGet and still got the same issue. Im using VS 2010 and targeting Any CPU,.NET 4.0 and I also open the same solution in VS 2012 as well – Aster Veigas Sep 25 '13 at 13:42
-
When I create a console app it works fine for me as well.But doesn't work with a wpf app referencing the same dll – Aster Veigas Sep 25 '13 at 13:50
-
1You need to provide more specifics to get help for your scenario. It has nothing to do with the app being a WPF or console app. It has most certainly something to do with your build configuration and your referencing libraries. – meilke Sep 25 '13 at 13:54
-
Try the same thing with a blank wpf app. You are most likely to get an exception ie BadImageFileExeption. My machine that im developing is a 64 bit and my app target platform is any cpu.So I installed the 32bit dll is that a oroblem? If I do install the 64 bit dll will it work on 32 bit machines? – Aster Veigas Sep 25 '13 at 13:55
-
You were right, I created another sample WPF app and the sql lite DB works fine. What could I be missing? This is the exception I get 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. – Aster Veigas Sep 25 '13 at 17:09
-
My guess is: Wrong native SQLite DLL for the build configuration used. Have you double-checked your build configuration for the whole solution and all the projects? – meilke Sep 25 '13 at 17:11
-
-
Please read this: http://stackoverflow.com/questions/6503634/64-bit-sqlite-dll-and-any-cpu. You have a 64-bit OS and therefore you probably need the 64-bit DLL. 32-bit systems need the 32-bit DLL. – meilke Sep 25 '13 at 17:16
-
Its finally working. Thanx a lot!! But this 64 bit dll would not work on 32 bit machines rite? So I gotta keep both the dll's? – Aster Veigas Sep 25 '13 at 18:07
-
1
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.

- 866
- 3
- 13
- 34