sqlite database location uwp - c#

I have an uwp app with a sqlite database that works as intended; I can close the program and restart it and the database is loaded properly. I want to look at the database with a database viewer but can't locate the file. I have tried unhiding files and searching for db.sqlite. Here is the code that connects to the database:
string path;
SQLite.Net.SQLiteConnection conn;
path =Path.Combine
(Windows.Storage.ApplicationData.Current.LocalFolder.Path,"db.sqlite");
conn = new SQLite.Net.SQLiteConnection(new
SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path);

C:\Users[Your User Name]\AppData\Local\Packages[Your Package Name]\LocalState
Your Package Name: You can find in file Package.appxmanifest in your Project
Your file db.sqlite will in folder LocalState

If you go to
C:\Users\[YourUserName]\AppData\Local\Packages\
your application will have its own folder there, which you may be able to recognise by name or do a search for sqlite, and the SQLite database file should be within your applications folder

Related

"Unable to open the database file". Do I have to put the full physical location of sqlite database when developing C# Console application

I have created a Sqlite database mydatabase.sqlite and placed it in my App_Data folder inside the console application project. I am getting an error:
Unable to open the database file
when developing a console application using sqlite and C#.
When I am using
string cs = #"Data Source=App_Data\mydatabase.sqlite;Version=3";
I am getting
Unable to open the database file error;
But when I am using
string cs = #"Data Source=C:\Users\UP_SW02\Desktop\ConsoleApplication1\ConsoleApplication1\App_Data\mydatabase.sqlite;Version=3";
The application is running properly.
Is there any possibility to use Sqlite Data Source not using full physical location?
Thank you..
All non-rooted paths used inside a program are relative to the folder in which the application is started. Note that this is not necessarily the folder that the application itself is in. You have to be very careful with such paths.
What you seem to be after is the folder relative to your own application. There's a simple method for getting that. If you have the Windows.Forms namespace included in your project, you can get the executable's path from System.Windows.Forms.Application.ExecutablePath:
// Get app folder
String appFolder = Path.GetDirectoryName(Application.ExecutablePath)
// Combine with sqlite db path
String dbPath = Path.Combine(appFolder, #"App_Data\mydatabase.sqlite");
// Build connection string
string cs = String.Format(#"Data Source={0};Version={1}", dbPath, 3);
An alternative for Application.ExecutablePath if you're not using a Windows forms program is Assembly.GetExecutingAssembly().Location, from the System.Reflection namespace.
The simplest way to get folder's relative path in a connection string is to simply add |DataDirectory|.
before
string cs = #"Data Source=App_Data\mydatabase.sqlite;Version=3";
after
string cs = #"DataSource=|DataDirectory|\App_Data\mydatabase.sqlite;Version=3";
No there is no way SQlite needs to full path address but you can resolve it with two solutions:
1- Save connection path in config file.
2- Get App_Data folder path by GetFolderPath method like:
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
So your code would be like:
var fileName = Path.Combine(Environment.GetFolderPath(
Environment.SpecialFolder.ApplicationData), "mydatabase.sqlite");
string sqliteConnection = string.Format("Data Source={0};Version=3;Timeout=2;", fileName);
3- Or the easy way is :
string sqlitePath = Directory.GetCurrentDirectory() + "/App_Data/mydatabase.sqlite";

How to change the default file/db location in Sqlite

I am creating an application using asp.net mvc 5. I want to use Sqlite in the application.
I have installed Sqlite using Nuget.
The default file path of the database file is C:\Program Files (x86)\IIS Express. I want to create the file in the App_Data folder inside the application itself.
Currently I am using the following code to create and open the DB file: SQLiteConnection sqlite_conn = new SQLiteConnection("Data Source=test_database2.db;Version=3;New=True;Compress=True;");
How can I change the default database file location in Sqlite?
We can create the DB file in App_Data folder using the below code
SQLiteConnection sqlite_conn = new SQLiteConnection("Data Source=|DataDirectory|test_database2.db;Version=3;New=True;Compress=True;");
Does anyone know how we can create the same file at some other location inside the application?

Release a WPF app with local database

OK, so I am quite lost. This is my first real application written for windows with a local database.
I have written an WPF application in VS2012 with a local database, which has all CRUD written as stored procedures.
The app is working as I run it in VS. But as I build the app and install it (on any machine) the database is not accessible. I have a feeling the connection string is causing this issue.
First I used this connection string:
SqlConnection conn = new SqlConnection(#"Data Source=(LocalDB)\v11.0;AttachDbFilename='D:\LH\Personalitytest win\DB.mdf';Integrated Security=True;Database=DB")
But as I got "Error: 26 – Error Locating Server/Instance Specified" I guessed that the Data Source of the connection is not correct (although the AttachDbFilename is definitly incorrect). So, I changed the connection string to:
SqlConnection conn = new SqlConnection(#"Server=(localdb)\v11.0;AttachDbFilename=" + AppDomain.CurrentDomain.BaseDirectory + "DB.mdf;Database=DB;Trusted_Connection=Yes;");
And I got an error which basically said that the file DB.mdf is not found.
So, I made sure that the .mdf and .ldf files are copied to the installation directory/database.
But after installation both these files are read only (even if these have read/write persmission as I am developing). I have tried to change the file permissions (for both files) as the application starts for the first time by:
string path = AppDomain.CurrentDomain.BaseDirectory + #"Database\DB.mdf";
FileInfo file = new FileInfo(path);
file.IsReadOnly = false;
and
File.SetAttributes(path, FileAttributes.Normal);
and
FileAttributes attributes = File.GetAttributes(path);
attributes = attributes & ~FileAttributes.ReadOnly;
File.SetAttributes(path, attributes);
but neither of these methods are working.
My question is basically, how should the database be implemented in the final release?
Your database should be located in your windows user directory
Other way is to install SqlServer Express on your machine. And configure your connection string accordingly.

C# application using database from wrong location

I have setup my sqlite database path as
string AppPath = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
dbName = AppPath + "\\data\\rbssystems.sqlite";
But when application is packed and installed using setup, my application uses
C:\Users\<username>\AppData\Local\VirtualStore\Program Files\RBS\data
it should be using
C:\Program Files\RBS\data
Can anyone tell whats going around and how to make it read database from
C:\Program Files\RBS\data
Thanks
Your app can't write to C:\Program Files unless it has administrative privileges. Windows automatically redirects you to C:\Users\<username>\AppData\Local\VirtualStore\Program Files instead. See this article for the explanation: http://blogs.windows.com/windows/archive/b/developers/archive/2009/08/04/user-account-control-data-redirection.aspx
Application data should always be in the AppData folder, never in Program Files.

Setting The Path of an db mdf file in local folder

How come The path I inserted in my Database Context is not working? Here's the code for my path
private string dbPath = #"TEST.MDF"
DataClasses1DataContext myDbContext = new DataClasses1DataContext(dbPath);
But when I run a query this gives me an error
An attempt to attach an auto-named database for file TEST.MDF failed. A database with the same name exists, or specified file cannot
be opened,
or it is located on UNC share.
And This is how My Folder looks like this
The mdf file is in the same location of my cs source code but the thing is they are not reading the path correctly.
my idea for this is that when I transfer to a diffrent pc I don't have to set up the paths again and again. are there any fix for this?
What about
private string dbPath = Application.StartupPath + "\\TEST.MDF";
But your Test.mdf isn't in the correct directory. Move it into \bin\Debug for this code to work.
Better you add your .mdf file in your project.. Add Existing Item=>Choose .mdf file from folder. After adding .mdf file in project, in Web.config or App.Config file connection string will be generate automatically and you can use that connection string to boot your store. Now as you build your project new .mdf file get copy into /bin/dubug folder and you dont need to write single line of code to connect your .mdf file.
Use this connection string :
SqlConnection connect = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename="+ AppDomain.CurrentDomain.BaseDirectory.Substring(0, AppDomain.CurrentDomain.BaseDirectory.Length - 10)+"sampleDatabase.mdf;Integrated Security=True");
Because bin\Debug\ string length = 10, this is why we subtract 10; and now you can get the solution path address and can connect the MDF database.

Categories