Sqlite pragma settings with SQlite for Windows Runtime - c#

We are trying to make Pragma settings for our SQLite database. We are writing a windows 8 app and use SQlite for Windows Runtime (so the .net wrapper for windows).
To try to optimise performance we want to set pragma synchronous = off, however when connecting using SQlite for Windows Runtime we cannot do this. The only connection we can make is by providing the Database Path and using for example:
var localConnect = new SQLiteConnection(SqliteData.DbPath);
(SqliteData is our class and it holds the path of course, we cannot pass anything but the path, so cannot add a connection string with settings with the path).
There is also the ability to pass SQLiteOpenFlags but none of these seem to relate to any of the settings we want.
What should I do to make this work?

You can specify it using Connection string.
String DBPath = #"Data Source=c:\temp\Sqlitedb.db;PRAGMA synchronous=OFF"

Related

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.

How to add connection string to once for whole solution to use?

Recently I am working on an .Net project. We used EF to handle SQL, when we make an installer of the program, we realize that app.config is visible which mean that the connection string is not safe.
I am looking for a way to add connection string (or maybe secret code and username) to the EF so that the connection string is not visible.
Something like change old code from this
Using db As ConnectDb.adoSentoEntities= New ConnectDb.adoSentoEntities
'TODO
End Using
to this
Using db As ConnectDb.adoSentoEntities= New ConnectDb.adoSentoEntities(ConnectionString)
'TODO
End Using
But since we used connect code to SQL all over the place, changing every single line of code is not possible. There is a way I only need to add connection string once?
You’d be better off encrypting the connection string section in the app.config. You wouldn’t need to make any changes.
Storing any sort of configuration in an assembly can be read using a hex editor.
It’s been answered on here before.
Encrypting Connection String in web.config
You’d be better off using a trusted connection if you’re using SQL Server. The user running the app would need to have permissions and no username and password is required.
Save connection string is settings of project properties.
Go in project properties.
Select settings.
Add new setting as connection string and save connection string.
Then you can use it for whole project.

How to embed SQLite database files into WPF application

I tried searching the net for this probably simple answer, but without success. I have my WPF app almost ready - using connection strings to connect to my 3 databases (EnteralDB, ParenteralDB, PatientDB) that have several tables.
Problem is that it works while debugging because I hard wired the connection string to specific location (my desktop)
using (SQLiteConnection con = new SQLiteConnection(#"Data Source= C:\Users\Peter\Desktop\EnteralDB"))
But how I can I make the connection string "universal" - meaning that it will work when I create setup for the app and it will install on a new computer? How to embed the database files into the project so they are PART of the project itself and then somehow point with the connection strings to them?
Thank you very much for help!
EDIT Actually, the comment by Clemens is correct. I changed the connection string to just the filename (EnteralDB.db), set copy if newer with content for the database properties and it is working

How to create dynamic database connection string C#

I just created a desktop Winforms application with localhost database.
The connect string I am using is this:
SqlConnection connect = new SqlConnection(#"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Administrator\Desktop\learningsystem\LearningSystem\LearningSystem\LearningSystem.mdf;Integrated Security=True");
If I want to run my application on other computers, how should I make it work?
EDIT:SOLUTION
Thank for all the help! I tried the following steps. I think it is working now. But please correct me if I did something tricky.
1. add a new setting item in project property setting. App.config will automatically update:
<connectionStrings>
<add name="LearningSystem.Properties.Settings.LearningConn" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\LearningSystem.mdf;Integrated Security=True;Connect Timeout=30"
providerName="System.Data.SqlClient" />
</connectionStrings>
2. In my program, just add the following statement to connect to the sql server
SqlConnection connect = new SqlConnection(#"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename=|DataDirectory|\LearningSystem.mdf;Integrated Security = True; Connect Timeout = 30");
Further question
If others will run this application on their computer(not in the same network), they just go into the project setting and change the value by selecting the database file I provide to them,the connectionString will automatically change, right?
Thanks!
It's generally a bad idea to hard code such stuff in your application. Normally, application settings and connection strings are placed in the application's configuration file (in the ConnectionStrings section).
Just like with all strings, you could build your connectionstring from dynamic parts (variables, settings, etc.) and then pass that generated connectionstring to the SqlConnection constructor. Again, to make those separate parts configurable without hard coding them in your application, you might want to add them to your application's configuration file (in the AppSettings section). But IMHO this is an overly complex solution in most scenarios. Putting the entire connectionstring in the ConnectionStrings section is more straightforward (and more flexible).
Anyway, again, to make your application configurable, you might use your application's configuration file (App.config or Web.config), you need to add a reference to System.Configuration in your project's .NET Framework dependencies and use the AppSettings and ConnectionStrings properties of the System.Configuration.ConfigurationManager class.
(Of course, there are more ways to make your application configurable. But using the application configuration file is one of the most straightforward solutions.)
Edit:
When deploying your app to another computer, you need to copy its database over too. If you want to use the application on multiple machines and let them connect to the same database, you might want to leave LocalDB and migrate the data to a SQL Server (Express) instance and make it accessible over the (local) network.
Edit 2 (regarding the recent edits in your post):
I see in step 1 that you are using an application setting (called LearningConn) in your solution now. That's fine. However, it is important that you also use that setting in step 2, like this:
SqlConnection connect = new SqlConnection(Properties.Settings.Default.LearningConn);
If you change the setting in Visual Studio, it will update the connection string. Since the setting will probably have application scope, it will not be possible to update the setting/connection string within your application in runtime (by the user).
I'm not sure if your connection string using |DataDirectory| will always work as expected in all scenarios. I have only been using it in ASP.NET webapplications. If it does work in WinForms applications, you might read this document to learn how to set it up. But personally I am somewhat sceptical about this approach.
I personally would opt for a solution where you use a placeholder in your connection string, which you replace with the full path to the .mdf file before you pass it to your SqlConnection constructor.
When you use "{DBFILE}" as the placeholder, for example, the value of your LearningConn setting would look like this:
Data
Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename={DBFILE};Integrated
Security=True;Connect Timeout=30
(Note that this value should be a single line without any line breaks!)
You might create a separate setting in your application called DbFile (of type string) to store the actual value that should be put in place of {DBFILE} in your connection string. When you use scope "user" for that setting, the value might be changed from within the application by the user. When saved, it might not be saved directly in the application's configuration file, however, but in an additional configuration file hidden somewhere in the user's Windows user profile. You might read this document to learn more about application settings.
Your code in step 2 might eventually look something like this:
string connectString = Properties.Settings.Default.LearningConn;
string dbFile = Properties.Settings.Default.LearningSystemDb;
connectString = connectString.Replace("{DBFILE}", dbFile);
SqlConnection connect = new SqlConnection(connectString);
To let your application's users select and store the database .mdf file to use, you might include (a variation of) the following code in your application somewhere:
using (var dlg = new System.Windows.Forms.OpenFileDialog())
{
dlg.Title = "Select database file to use";
dlg.Filter = "Database Files (*.mdf)|*.mdf";
dlg.CheckFileExists = true;
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Properties.Settings.Default.DbFile = dlg.FileName;
Properties.Settings.Default.Save();
}
}
Your question is not clear!
you need work with one Database on 2 or more PC?!
OR
you need work with 2 separate programs?
if you need 2 separate programs :
you must copy .mdf file to other PC at same address or keep mdf address in app.config and read it before connect to SQL.
How to Read From app.config
if you need work with one Db you must connect to dataBase Server such as SQL Server and keep connection string in app.config in connectionStrings tag.
Get connection string from App.config
If you want to work on other PCs, rather than building it dynamically make the connection string more generic:
Server=(localdb)\\mssqllocaldb;Database=LearningSystem;Trusted_Connection=True;MultipleActiveResultSets=true
This should create the mdf file under 'mssqllocaldb' in %appdata% for each user. You might need LocalDb installed (which you tick during SQL Server installation)

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.

Categories

Resources