如何获取数据库名称 C# Winforms LocalDb v11.0

How to get name of database C# Winforms LocalDb v11.0

我正在尝试以编程方式恢复数据库;为此,我需要知道数据库的名称。

到目前为止我所做的是:

string con = "Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\BbCon.mdf;Integrated Security=True;Connect Timeout=30;";

System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder(con);

SqlConnection conn = new SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\BbCon.mdf;Integrated Security=True;Connect Timeout=30;");

conn.Open();
string server = builder.DataSource;
string database = builder["Database"] as string;

MessageBox.Show(database.ToString());

但是当我尝试访问消息框中的数据库字符串时,我得到一个空结果。请帮助我找出问题所在

未指定数据库,您必须添加:

Database=myDataBase

到您的连接字符串:

string con = "Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\BbCon.mdf;Integrated Security=True;Connect Timeout=30;Database=myDataBase";