0

I am trying to publish my c# project and I am worrying that if I publish it my connection to the database will not work once I install the project to another machine. Someone told me that I could use

and it seems not working for me Application.StartupPath I have this connection string for my database

string connectionstring = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True";

and when I put the Application.startupPath in the connection string like this

string connectionstring = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|"+Application.startupPath+"\Database1.mdf;Integrated Security=True";

it is not working

Glen Thomas
  • 10,190
  • 5
  • 33
  • 65
  • 1
    Surely you want Application.startupPath+"|DataDirectory|.. – BugFinder May 16 '17 at 10:11
  • add your connection string in web config or app config. then on each machine you can change those connection string values in this file – Bhuban Shrestha May 16 '17 at 10:11
  • What is the value of `Application.startupPath` ? – Koby Douek May 16 '17 at 10:11
  • The startup path is where the application is stored, not its data. Otherwise, deploying a new version would delete existing data. Instead of worrying *try* your deployment without modifications. I strongly suspect that Windows will create an application data folder inside `ProgramData` anyway – Panagiotis Kanavos May 16 '17 at 10:13
  • thanks for you answer guys. but i dont know what to do exactly. im new in this (publishing project and putting it in another machine) so basically im clueless what to do. if possible please give me example base on my connection string given above. what i want to do here is once i put it in another machine i will not encounter any error because of the database is not located in the same path before. – nica garcia May 16 '17 at 10:26
  • string connectionstring = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename="+Application.StartupPath+"/Database1.mdf;Integrated Security=True"; – nica garcia May 16 '17 at 10:33

0 Answers0