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?
Related
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";
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
Hi I am trying to get all history url of mozilla firefox. To do that I am accessing places.sqlite file inside %appdata%mozilla/firefox/profile/*.default folder. I am using system.data.sqlite dll in my code and tried x64 and x86 both of the solution. As I am trying to connect the database it is showing me an exception that the file is encrypted or not a database file.
To cross check this exception I've installed a dbmanager for sqlite and opened it. This is showing me all the data that I want to show. But I am not able to get the same data using c# application. Please Help me to get this thing resolved.
SQLiteConnection sqlite_connection = new SQLiteConnection("Data Source=" + dbPath + ";Version=3;New=True;Compress=True;Integrated Security=SSPI;");
SQLiteCommand sqlite_command = sqlite_connection.CreateCommand();
sqlite_connection.Open();// exception occurs here
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.
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.