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";

3
  • Why would you need to change the connection string on every computer? Commented Jun 22, 2013 at 10:01
  • You can investigate if |DataDirectory| can help you. See this question Commented Jun 22, 2013 at 10:02
  • Beacuse of this: C:\Users\Gal\Desktop\WebApplication2\WebApplication2, in every pc, its diffrent Commented Jun 22, 2013 at 10:06

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You should use |DataDirectory| as that is standard way of dynamically pointing to your App_Data folder in .NET connection strings.
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";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.