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"
Related
My application is working in debug mode or from Visual Studio. But after deployment / publish, we are not able to connect to the database from setting file connection string.
BILLING_SYSTEM.Settings setting = new BILLING_SYSTEM.Settings();
We are using
string connectionString = setting.ConnectionString_Web.ToString()
SqlConnection conn = new SqlConnection(connectionString);
Connection String is:
Data Source=.;Initial Catalog=<databaseName>;Integrated Security=True
Its working on local. But after deploy on another machine not able to connect database.
Is there any method in set wizard which resolve my problem?
Please suggest. Thanks in advance.
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 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...)
I have been working on a project related to database (.mdf). I have created some windows forms in visual studio using C#. Basically these forms work together to store, update and delete data from the Service Based Database i created.when I run this project in another PC there is problem in connection to data base,I change the connection string as in another PC but the smae problem.
SqlConnectionStringBuilder s = new SqlConnectionStringBuilder("Data Source=.\\SQLEXPRESS;AttachDbFilename=H:\\PDF_to_TEXT_PROJECT_MBRM\\PDF-to-Text Convertor\\PDF-to-Text Convertor\\Database1.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
can you tell me how solve this problem?
this error:
An attempt to attach an auto-named database for file H:\PDF_to_TEXT_PROJECT_MBRM\PDF-to-Text Convertor\PDF-to-Text Convertor\Database1.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
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.