0

I build a register but every time I move pc, I need to change the string connection.

This is my code: http://pastebin.com/3CN3TmdB

How can I not have to change the data source every time I'm changing computer? If anyone could help me, that would be great! Thanks for any help.

This is my connection string:

string connection = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Gal\Desktop\WebApplication2\WebApplication2\App_Data\MyDB.mdf;Integrated Security=True;User Instance=True";

suspectus
  • 16,548
  • 8
  • 49
  • 57
Gal Israel
  • 83
  • 1
  • 2
  • 7
  • Why would you need to change the connection string on every computer? – Oded Jun 22 '13 at 10:01
  • You can investigate if `|DataDirectory|` can help you. See this [question](http://stackoverflow.com/questions/1409358/ado-net-datadirectory-where-is-this-documented) – rene Jun 22 '13 at 10:02
  • Beacuse of this: C:\Users\Gal\Desktop\WebApplication2\WebApplication2, in every pc, its diffrent – Gal Israel Jun 22 '13 at 10:06

2 Answers2

2

You should use the |DataDirectory| placeholder:

string connection = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MyDB.mdf;Integrated Security=True;User Instance=True";

.NET will replace the value of |DataDirectory| at runtime with the path to your App_Data folder.

Erik Schierboom
  • 16,301
  • 10
  • 64
  • 81
  • You should use `|DataDirectory|` as that is standard way of dynamically pointing to your `App_Data` folder in .NET connection strings. – Erik Schierboom Jun 22 '13 at 10:11
1

you can put connection string in a seperate file you can use udl file steps for use udl: 1-create text file 2-change the extension of text file to .udl 3-open udl file and choose provider then connection parameters 4-save and close file

in c# code you can refer to this file string connection = @"File Name=myudlFileName.udl";