Problems with SQLite ADO.net connection and Deployment - c#

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?

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.

Related

ASP.NET MVC C# change code from accessing a mySQL database to a MSSQL database

I have an application that I used to connect to a mySQL database. I have installed the MySQL.Data package and I use this code to build my connection:
services.AddScoped<System.Data.IDbConnection>((s) =>
{
IDbConnection conn = new MySqlConnection(Configuration.GetConnectionString("databasename"));
conn.Open();
return conn;
});
This works fine as I am able to read/write data from the database.
Now, I need to have the same application access a MS SQL database. The tables are all the same and the fields on each table are the same as well. I am having a hard time finding what I should change my code to (or what package I should include). When I remove the MySQL.Data package, the MySqlConnection function becomes invalid (as would be expected).
Any idea what package to include and what function to use to establish the connection?
Any assistance is greatly appreciated.
Thank you!
EDIT #1
I found an old stackoverflow post (I should have referenced it here but I closed the window) that talks about this very issue. The suggestion was to add
using System.Data.SqlClient;
and then change the assignment code to
IDbConnection conn = new SqlConnection(Configuration.GetConnectionString("databasename"));
I made these changes and now I get this error within the code:
Im at a loss as to how to resolve this. I verified that my project is in framework .NET core 3.1
Not sure about size of your project and detailed requirements but I would suggest to use libraries like:
Entity Framework - no SQL knowledge needed, can connect to any Db technology, almost no code changes required when switching between Db providers
or
Dapper - fast lightweight also supports multiple Db providers but you need to write correct SQL commands yourself specific for each Db technology you use

System.Data.OleDb.OleDbException: 'Could not find installable ISAM.' - MS Access (.accdb) and C# (.Net Framework)

There were few similar questions here, but i couldn't locate any with acceptable answer. or that it would actually work one.
I have create a local storage as .accdb file and tried to connect witch c# to it.
In summary those are the parts needed:
public static string accessConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;DataSource=C:\MS_Access_Test_DB.accdb";
OleDbConnection connection = new OleDbConnection(accessConnectionString);
connection.Open();
It always crashes, with an error from description. I had tried to use apostrophes, quotes etc for C:\MS_Access_Test_DB.accdb
I had also tried JET.4.0 with same error & adding Persist Security Info=True; into the mix.
Ok,
The issue as pointed out by Steeeve & Panagiotis Kanavos using is access driver 32bit when i use 64bit architecture, and this will not work. Especially in office/corporation that already has a lot of build in add-ins for office 32 bit. There is also workaround, to install both drivers, but it would require each user to have it, therefore it wasn't solution in my case.
Id much prefer using SQLite, but the tables have to be connected to PowerBI i now that SQLite have to use external drivers to update datasets, therefore for now, this solution was also out.
Considering I cannot force each user to change drivers etc. I tried and saved the access file as .mdb (2002-2003).
So it's either: intalling 64bit driver (proposed initialy) or a workaround (with .mdb):
public static string accessConnectionString = #"Provider=Microsoft.JET.OLEDB.4.0;Data Source=C:\MS_Access_Test_DB.mdb";
OleDbConnection connection = new OleDbConnection(accessConnectionString);
connection.Open();
Or:
public static string accessConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\MS_Access_Test_DB.mdb";
OleDbConnection connection = new OleDbConnection(accessConnectionString);
connection.Open();
Important note, there is lot of .accdb functionalities that cannot be used in .mdb, so if you have to use access, than be carefull. If you can, consider SQLite/SQL SE proposed in comment's under my question.
EDIT: I had also messed up the first time... Instead of DataSource I should have had Data Source so it seems to work, despite using Driver 32bit against 64bit app.

Local DB in Visual Studio 2015

After a few years, I have returned to writing in C# and I am really struggling here - I would like to have my app to have a local SQL database. I have added Service-based database and "Database1.mdf" was added to my project. I created a table and added some data just to see if it is working but I cannot connect to it. I tried numerous connection strings with no success (Server not accessible).
Do I need to run something else in the background? I thought that I might have a local database and with .NET client I can access it, and I hoped it would work whenever I bring my application (also not requiring any SQL server running). Is that wrong?
If you don't require any SQL server, take a look at SQLite. This is lite SQL database engine. Database is just one file. C# has a great library to SQLite on NuGet: https://www.nuget.org/profiles/mistachkin
SQLite is widely used, event in Android (as a native db engine).
here is what i use to connect. it appears as a Data Connection in Server Explorer.
string con2 = #"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=" + Application.StartupPath + "\\jobfile_2017.mdf;Integrated Security=True;Connect Timeout=30";
when i first started working with these, i used this source 1
it works on PC's that i have nothing installed (not even office) but as i said i'd be interested to know of any shortcomings of this method
I experiencing same problem and decided to move mdf file to static c:\db directory. Connection string was changed to incorporate new file location (AttachDbFile).
But AttachDbFile has some issues also (by some reason one table in db is inaccesible and error is access denied).
So I decided to move to Sqlite. LocalDb has many issues to work with. I read good note to resolve problem: in command line stop/remove/start name of instance. But it nuissance.
Wish you good luck to work with these db files.

Connecting to an SQL database in c# [duplicate]

This question already has an answer here:
SqlConnection not being able to open the connection
(1 answer)
Closed 6 years ago.
I am trying to connect to my free sql database that I opened in: www.freesqldatabase.com/account/.
When using this www.phpmyadmin.co/ admin tool I can get into the databse, add tables and all that. But i can't connect to this database from my project.
using this code:
SqlConnection sql = new SqlConnection("Server=sql7.freesqldatabase.com;Database=sql7115***;User Id=sql7115***;Password =*****;");
sql.Open();
I get an SqlException saying that "Could not find the server".
Am I missing something?
By the way i am getting those warnings
and if i click "here" i see this.
P.S:
When i ping the host with the right port (that was sent to me with the email) I get a reply, so it is probably listening.
At the moment, the service you are using only offers MySql databases (though they intend offering MS SQL Server databases in the near future.
The problem is that the SqlConnection class you are using is deliberately tailored to MS Sql Server Databases, and - as I just said - that's not what you are talking to.
You will want to find an ADO.Net solution for MySQL - something like this.
Alternatively, you might be able to use OLEDB if you have suitable drivers installed...
You can use the NuGet Package MySql.Data. To use it in your project insert following in the Package Manager Console.
Install-Package MySql.Data
After that you can use the class MySqlConnection.
Don't forget to include the reference in your class!
using MySql.Data;
using MySql.Data.MySqlClient;
You should be able to establish a connection like this:
string connectionString = "server=sql7.freesqldatabase.com;user=sql7115***;database=sql7115***;password=******;";
MySqlConnection mySqlConnection= new MySqlConnection(connectionString);
mySqlConnection.Open();
For further information you can look in the tutorial.

Firebird error : "operating system directive CreateFile failed" on opening connection

I have narrowed done my problem to small sample where this issue can be easily produced.
Brief -
I am using embeded dll's of firebird for database connectivity i.e. fbembed.dll to connect to firebird databases.
In code, i have three connection strings like this (note additional attribute client library specified in third connection string) -
private const string connection1 = #"User=SYSDBA;Password=masterkey;Database=D:\DB1.fdb;ServerType=1;Charset=UTF8";
private const string connection2 = #"User=SYSDBA;Password=masterkey;Database=D:\DB2.fdb;ServerType=1;Charset=UTF8";
private const string connection3 = #"User=SYSDBA;Password=masterkey;Database=D:\DB2.fdb;ServerType=1;client library=D:\fbembed.dll;Charset=UTF8";
I have two buttons on UI and on first button click i have this code -
FbConnection fbConnection = new FbConnection(connection1);
fbConnection.Open();
On second button click i have this code -
FbConnection fbConnection = new FbConnection(connection3);
fbConnection.Open();
If i create connection with connection string specified as connection3, i am getting FbException "operating system directive CreateFile failed".
However, if i replace the connection string as connection2, it works fine.
Moreover, if i create connection with connection3 and then with connection1 no issue but in case i create connection with connection1 and then connection3, same fbexception coming.
Note, the database specified in connection2 and connection3 is same but only difference is of additional attribute client library.
Why this strange behaviour with embeded firebird. It all works fine in case i have firebird server installed on my system. But, i want to know the issue why it persists in embeded dll's of firebird?
Your issue is related to: http://tracker.firebirdsql.org/browse/CORE-2507
This is a bug in FB engine, fixed in 2.1.4 version.
The problem is that you are connecting to database with Firebird server and Firebird embeeded. The first one locks the database file for open with other proceses(i think you are using superserver).
Another option is that you are using two fbembed.dll from different locations.
This is rather funny but I fixed my Firebird 1.5 + Windows 7 problem!
Installed Firebird as application then ran fb application as admin and the batch file (gbak.exe -b -v -g) also as admin.
Got rid of:
**ERROR**:uavailable database
or
operating system directive CreateFile failed

Categories

Resources