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
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
public static string GetFilePathFromConnectionString(string connectionString)
{
var attachDbFileName = new SqlConnectionStringBuilder(connectionString).AttachDBFilename;
return attachDbFileName.Replace("|DataDirectory|", AppDomain.CurrentDomain.GetData("DataDirectory").ToString());
}