Reputation: 439
Because of what seem to be overwhelming difficulties around Entity Framework, I’m in the process of moving to Dapper. I have an application that uses both a remote MS SQL Server database and a small local database (SQLite). Dapper against the MS SQL Server database is working great, but for some reason I can’t get SQLite connection working.
From the way I understand it, Dapper simply needs an ADO connection. I installed a NuGet package “sqlite-net”, and used the following code to create the connection:
SQLiteConnection con = new SQLiteConnection(“data source=C:\Data\OneDrive\Data\TestDB.sqlite”);
and
SQLiteConnection con = new SQLiteConnection(“data source=C:\Data\OneDrive\Data\TestDB.sqlite; Version=3”);
Neither one of these work and they throw an exception “Could not open database file”. The file SQLite.cs (coming from the NuGet package) threw the error.
Because of my past problems with deployment, I’ve hesitated installing the downloaded files from System.Data.sqlite.org.
Please can someone help me?
Upvotes: 0
Views: 304
Reputation: 11273
Looking at the nuget package and specifically the SQLite.cs file:
https://github.com/praeclarum/sqlite-net/blob/master/src/SQLite.cs
I see the following constructor definition (line 180):
public SQLiteConnection (string databasePath, bool storeDateTimeAsTicks = true)
: this (databasePath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create, storeDateTimeAsTicks)
{
}
This shows that the first argument is just the path, and should be called like:
SQLiteConnection con = new SQLiteConnection(@"C:\Data\OneDrive\Data\TestDB.sqlite”);
The @ sign escapes the \ characters from being interpreted as character codes. These two issues look like the problem that you have and I would try it out and see if it works.
Upvotes: 2