0

How to get the filepath to the .mdf file of a LocalDB database from its connection string in .NET?

Example string:

Data Source=(LocalDb)\v11.0;Initial Catalog=MyDb;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\MyDb.mdf
0

1 Answer 1

1
public static string GetFilePathFromConnectionString(string connectionString)
{
    var attachDbFileName = new SqlConnectionStringBuilder(connectionString).AttachDBFilename;
    return attachDbFileName.Replace("|DataDirectory|", AppDomain.CurrentDomain.GetData("DataDirectory").ToString());
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.