C# - Access SQLite database for WPF application - c#

I have create Set up file for WPF application and the database is SQLite DB. After the installation the SQLite DB is located this path in C folder,
C:\Program Files (x86)\myCompany\myScanApp.
Then I have written connection string like this way for establishing a connection to SQLite DB,
static SQLiteConnection dbConnection = new SQLiteConnection(#"Data Source=C:\Program Files (x86)\myCompany\myScanApp\test.s3db;");
Then I have created a Setup file again and then run this application on another PC. But, it is failing to run in the PC. How can I access DB in application?

using(SQLiteConnection conn= new SQLiteConnection(#"Data Source=C:\Program Files (x86)\myCompany\myScanApp\test.s3db;"))
{
conn.Open();
SQLiteCommand command = new SQLiteCommand("Select * from yourTable", conn);
SQLiteDataReader reader = command.ExecuteReader();
while (reader.Read())
Console.WriteLine(reader["YourColumn"]);
reader.Close();
}
Something like this should work. Here whole article Getting started with SQLite in C#

Related

Not able to connect access database to visual studio

I am trying to conncet access database to my visual studio but i face this type of error "The Microsoft.ACE.OLEDB.12.0" provider is not registered on the local machine.
I look at some solutions on google i got that after downloading Microsoft Access Database Engine on Local machine we are able to connect database.But i still face this problem. So What should i do for it? .And i am using Windows10 but i use Ms office 2007 and my system is 64-bit.enter image description here
You don't mention if you planning to use the x32 bit or x64 version of access.
So, assuming you downloaded the access data engine? You have to choose or decide WHICH bit size version you wish to use.
That download can be found here:
https://www.microsoft.com/en-ca/download/details.aspx?id=13255
And you have two choices:
Ok, next up?
You MUST set and FORCE your project to the correct bit size.
that is this project setting in VS.
So, if you using x32 version, then use x86 version, and if adopting x64 version, then set your project to x64.
So, don't use "any" for your project setting - you have to force the issue.
So, try setting up a connection. You have two choices:
Use the ODBC provider,
or
Use the oleDB provider
So, ok, ODBC? then that connection setting should look like this:
For odbc, the connecton string should look like this:
Driver={Microsoft Access Driver (*.mdb, *.accdb)};
dbq=C:\TEST\test444.accdb;uid=Admin
for oleDB, the connection string should look like this:
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\test\test45.accdb
And in both cases, you can as a normal rule, leave the password blank.
Assuming you used the project settings, say here:
(and you can use the connection string builder in above. BUT BE VERY carefull, if you are running access x64, then the test connection WILL ALWAYS ALWAYS fail, since VS is a x32 bit applcation, but if you run your code, and the project was forced to x64 bits, then the connection will work DISPITE VS and the test connection telling you it does not.
So, to use the above ODBC connection, we have this:
using (OdbcConnection conn = new OdbcConnection(Properties.Settings.Default.AccessODBC))
{
string strSQL =
"SELECT * FROM tblHotels ORDER BY HotelName";
using (OdbcCommand cmdSQL = new OdbcCommand(strSQL, conn))
{
conn.Open();
DataTable rstData = new DataTable();
rstData.Load(cmdSQL.ExecuteReader());
dataGridView1.DataSource = rstData;
}
}
And for oledb, we would have this:
using (OleDbConnection conn = new OleDbConnection(Properties.Settings.Default.AccessDB))
{
string strSQL =
"SELECT * FROM tblHotels ORDER BY HotelName";
using (OleDbCommand cmdSQL = new OleDbCommand(strSQL, conn))
{
conn.Open();
DataTable rstData = new DataTable();
rstData.Load(cmdSQL.ExecuteReader());
dataGridView1.DataSource = rstData;
}
}
So, which provider to use?
Most use and suggest oleDB.
However, I think using ODBC is not all that bad of a choice. the reason is you can change the connection to sql server, or other database systems, and NOT have to change your provider, but only the connection string.
While you can sort of do the same with oleDB, there is now MUCH better support for ODBC database systems.

Local Database fails and crashes in VS2017

I'm trying to set up a local database (Service-based Database) in my C# app. When I intially adds the database to the project, I can see it and edit tables in both VS and SSMS, but when I try to connect to it programmatically, I get this error:
"Cannot open database \"[DataDirectory]DATABASE.MDF\" requested by the login. The login failed.\r\nLogin failed for user 'DESKTOP-MM6AR72\\majbom'."
And after that, the database is gone from both VS and SSMS.
I'm connecting this way:
using(SqlConnection conn = new SqlConnection("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=[DataDirectory]DATABASE.MDF;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"))
{
using(SqlCommand cmd = new SqlCommand(SQL, conn))
{
cmd.Connection.Open();
}
}
I have never tried to use databases this way, so maybe I'm doing something wrong. What I'm trying to accomplish, is a way of storing a lot af data temporarily in my app and make it searchable and editable.
EDIT:
I've followed this guide: https://learn.microsoft.com/en-us/visualstudio/data-tools/create-a-sql-database-by-using-a-designer
Thanks in advance

Database connection to access in C#

I am trying to use an access database to create a login for a Hotel reservation system in C#, unfortunately I have run into a problem with the following error;
The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.
Most of the research I done with this was that people did not have Access installed on their machines. But as I do have it installed and still get this problem I am getting very confused over this. If anyone has any suggestions or solutions to this I would be grateful!
I am using a file reader class and for any of the methods in the class it always displays the error at the "Conn.Open()" command, here is my code for one of the methods.
public DataTable LoadLogin()
{
string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:"Not getting my connection string :p";
DataTable results = new DataTable();
using (OleDbConnection conn = new OleDbConnection(connString))
{
OleDbCommand cmd = new OleDbCommand("SELECT User_Name, Password FROM Employee", conn);
conn.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
adapter.Fill(results);
}
return results;
}
This is likely due to the fact that you are compiling and running 32-bit, but are using the 64-bit Access driver. The other possibility is that you have the 32-bit version of Access installed in which case you need to compile and run your program for x86 using "Microsoft.Jet.OLEDB.4.0" as your provider string.
Unfortunately, you can't mix-and-match the bitness of your program with the bitness of the Access provider you are using.

what is the correct path of Access database when deployment an application

I did an application in c# language using visual studio 2010.
I used this connection statements to connect to an Access database:
OleDbConnection connect = new OleDbConnection();
connect.ConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=..\\Data\Database1.accdb;Persist Security Info=False;";
connect.Open();
command = new OleDbCommand();
command.Connection = connect;
command.CommandText = "Sql query";
reader = command.ExecuteReader();
connect.Close()
when I deployed the application and then execute I got some errors ( database not found, the path of the database not correct).
My question is what is the correct path that should I used to reach the database after deployment the application?
The correct path is the path from the location where the EXE is running, or where the web app is running to the database (the path you have shown is a relative path - the double-dot-double-slash means 'go up one folder from where you are running'.

Connecting to SQL Server database with Mono C#

I am trying to migrate my service cross-platform with mono, but in attempting to connect to a SQL Server database I get the following timeout error
Timeout expired. The timeout period elapsed prior to the completion of the operation or the server is not responding. at Mono.Data.Tds.Protocol.TdsComm..ctor
at System.Data.SqlClient.SqlConnection.Open()
Databases are fairly new to me, but as far as I can tell from here (Google cache page, mono site is down) accessing SQL Server databases is now possible in Mono. Is that correct?
I attempted to structure my connection string as shown, but still no luck. My simple test code...
using (SqlConnection con = new SqlConnection(cs))
{
con.Open();
SqlDataReader reader = null;
SqlCommand cmd = new SqlCommand("SELECT Parameter FROM Deltas", con);
reader = cmd.ExecuteReader();
reader.Read();
Console.WriteLine(reader["Parameter"].ToString());
con.Close();
}
Am I missing any references or is my format incorrect? How can I connect using Mono C#?
EDIT:
Connection String, Defined globally and init in constuctor:
cs = #"Server=xxx.xxx.xxx.xxx;
Database=myDB;
User ID=user;
Password=passwd;";
cs = #"Data Source=xxx.xxx.xxx.xxx;
Network Library=DBMSSOCN;
Initial Catalog=myDB;
User ID=user;
Password=passwd;";
Top is me trying to conform to the mono example, bottom is what works with the .NET runtime.
It seems you hit a bug:
http://www.mail-archive.com/mono-bugs#lists.ximian.com/msg50686.html
quote:
This only happens when using the .NET 2.0 version of TdsComm - when
compiling with .NET 1.0 (mcs instead of gmcs), the connection also
works.

Categories