0

I am trying to make a portable connection string with a service-based database.

If I use

C:\Users\X\Desktop\C# Projects\Windows Form Applications\PortableConnectionString\PortableConnectionString\BazaDate.mdf

and not |DataDirectory|BazaDate.mdf, it works.

What can I do to make it portable?

SqlConnection conn= new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\BazaDate.mdf;Integrated Security=True;");
conn.Open();
1
  • This isn't a right practice. you are exposing your filename/path. Commented Oct 20, 2019 at 11:31

1 Answer 1

2

Environment.GetCommandLineArgs()[0] is the full path of your executable, thus:

string appPath = Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]);
string connStr = $@"Data Source=(LocalDB)\v11.0;AttachDbFilename={appPath}\BazaDate.mdf;Integrated Security=True;";

var conn = new SqlConnection(connStr);
conn.Open();
Sign up to request clarification or add additional context in comments.

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.