error in Connection String in C# - c#

I am new to C#. I want to use my Microsoft SQL Server database file test.mdf in my output software in C#. In the past, I had just copied the connection string in Visual Studio like this :
Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Home\Documents\test.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True
as you see the database file path is : C:\Users\Home\Documents\test.mdf;
When I create setup for my sofware in Visual Studio 2008, and install the software on another PC, it errors :
An attempt to atach an auto-named database for file C:\User\Home\Document\test.mdf failed ...
So I want to address the file with the installation folder path whith this :
string dir = Application.StartupPath + "\\" + "test.mdf";
but when I want to run program in Visual Studio 2008 it erros
string dir = Application.StartupPath + "\\" + "test.mdf";
SqlConnection con = new SqlConnection(#"Data Source=.\SQLEXPRESS;AttachDbFilename=" + dir + ";Integrated Security=True;Connect Timeout=30;User Instance=True");
Error 1 A field initializer cannot reference the non-static field,
method, or property
'phonebook.Form1.dir' C:\Users\Home\Documents\Visual Studio
2008\Projects\phonebook\phonebook\Form1.cs 25 95 phonebook
UPDATE
When I use
SqlConnection con = new SqlConnection(#"Data Source=.\SQLEXPRESS;AttachDbFilename="+ Application.StartupPath +" \\test.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
it errors :
One or more files do not match the primary file of the database. If
you are attempting to attach a database, retry the operation with the
correct files. If this is an existing database, the file may be
corrupted and should be restored from a backup. Cannot open user
default database. Login failed. Login failed for user 'Home-PC\Home'.
While I have copied right test.mdf file there

As the error message says, you can't use the value of one instance field when initializing another. You probably don't want dir as a field anyway. Just move all of this into the body of the constructor... or ideally, only create your SqlConnection when you need it anyway. Don't use a single instance throughout your application, but go through a "create, use, dispose" cycle every time you need database access. (Ideally, don't do this in your GUI code, either...)

Related

How can i make a relative path for my database to my "mainfolder" so i can run my project on another computer?

I created a database, I put it into my bin/debug folder, I made the connection with |DataDirectory|, relative path, and when I try to open the project on another pc it freezes.
string constr = #"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\estic.mdf;Integrated Security=True;Connect Timeout=30";

System.Data.SqlClient.SqlException with VS 2017

I've got a problem with a project with Visual Studio 2017.
I want to save data in a local SQL Server database but when I run the script this error always happens at connection.open();:
System.Data.SqlClient.SqlException: "An attempt to attach an auto-named database for file C: \Users\mrman\onedrive\dokumente\visual studio 2017\Projects\WindowsFormsApp1\WindowsFormsApp1\DB_Nährwertrechner.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share."
Here's the code for the connection:
SqlConnection connection = new SqlConnection(#"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C: \Users\mrman\onedrive\dokumente\visual studio 2017\Projects\WindowsFormsApp1\WindowsFormsApp1\DB_Nährwertrechner.mdf;Integrated Security=True;Connect Timeout=30");
connection.Open();
Do you guys know why that error comes and how to solve it?

connection string with relative path to database not working?

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...

Access DB connected with local directory

Basically I want the connection of my Access DB always available, so if i move the folder project to another computer it has to keep working, without changing the folder path.
I'm working with Windows Forms in Visual Studio 2012.
conn.ConnectionString = #"Provider=Microsoft.Jet.OLEDB.4.0;Data source= Z:\Tempesta\Area Progetto\Area_Progetto_27_02_2014\Area_Progetto_DATA_MAGAZINE\Data_Magazine\Data_Magazine\DB\DataMG.mdb";
That's the code I have right now for the connection to the DB.
Try this
conn.ConnectionString = #"Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=|DataDirectory|DataMG.mdb"

Connection string to access database in Program files

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.

Categories