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

时间:2017-02-18 16:13:09

标签: c# sql-server localdb

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

到目前为止,我所做的是:

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());

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

1 个答案:

答案 0 :(得分:3)

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

Database=myDataBase

连接字符串:

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