5

How to deploy the win-form application with the .mdf file,i have taken the setup file and also add the .mdf file and .ldf file, when running in visual studio its working fine after taking the setup the record its not showing.Even not saving also in the database after taking the setup file. How to attach the database with my setup file.Any ideas...

leppie
  • 115,091
  • 17
  • 196
  • 297
Sarvan
  • 137
  • 4
  • 16

1 Answers1

1

the database mdf file is not a project output file. So, you can move it to resources and add resources from the project output window or your can directly add in application folder from the Application Setup Wizard.

enter image description here

Select the file from the Browsing File Dialog window. If you would like to add all local resources in the setup file then you can select Localize Resources from the project Output window.

enter image description here

//"Data" folder should be created in Application Folder path
String DBPath = Application.StartupPath + "\\Data\\CMM.mdf";
String ConnString = String.Format("Data Source=.\\SQLEXPRESS;AttachDbFilename={0}; +
                    "Integrated Security=True;User Instance=True", DBPath);
SqlConnection con = new SqlConnection(ConnString);
Shell
  • 6,818
  • 11
  • 39
  • 70
  • how to get the connection string,here is my connection string is SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\CMM.mdf;Integrated Security=True;User Instance=True"); this connection i am using – Sarvan Mar 19 '14 at 08:29
  • when running in vs2010 also the data is not saving in the mdf file. – Sarvan Mar 19 '14 at 08:30
  • when i used the above connection string i got this error message An attempt to attach an auto-named database for file {0} failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share. – Sarvan Mar 19 '14 at 09:25
  • {0} is the parameter in the string that will be replaced with `DBPath` variable using `String.Foramt` function. Have placed all of three lines? – Shell Mar 19 '14 at 09:28
  • String DBPath = Application.StartupPath + "\\Data\\CMM.mdf"; String ConnString = String.Format("Data Source=.\\SQLEXPRESS;AttachDbFilename={0};" +"Integrated Security=True;User Instance=True", DBPath); SqlConnection con = new SqlConnection(ConnString); i am getting this error Error"A field initializer cannot reference the non-static field, method, or property 'SetupCreationTest.Form1.ConnString'" – Sarvan Mar 19 '14 at 09:31