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.
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";
Here is my connection string:
SqlConnection myConnection = new SqlConnection(#"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\m_bou\Desktop\PCOTools\Numbers.mdf;Integrated Security=True;Connect Timeout=10");
I have tried using the |DataDirectory|\Numbers.mdf, and also many other options people have tried but no luck! I just want it to load the mdf file that is in the executable directory when it loads... in my case its the debug folder, but when I deploy on a different machine it needs to be wherever the exe resides. Can anyone tell me what i'm doing wrong? This is a Winform applicagtion...
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
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.
I am working on windows form which uses microsoft access database.
When the application will be installed, the database will be on this location
C:\Program Files (x86)\Amrit\trial\Database.
How can i make this work so that this application will linked to the database when installed in my computer.
Can somebody give me easy solution..
Currently my connection string looks like this..
string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Amrit\\Desktop\\Database.accdb ;Persist Security Info=False;";
when i create setup file for appplication, the database will be in ProgramFiles/AmritCreations/AppName/Database.accdb
Just place your database and your application in the same directory and use this connectionString
string connString = "Provider=MICROSOFT.ACE.OLEDB.12.0; " +
"Data Source=|DataDirectory|/Database.accdb";
In times you're creating a Setup Project, try to place the database file and [yourProjectName].exe in the same directory.