shahid khan
shahid khan

Reputation: 439

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

I trying to restore database programatically; for that I need to know the name of the database.

What I have done so far is this:

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

but when I try to access database string in the message box, I get an empty result. Please help me to find what my problem is

Upvotes: 0

Views: 622

Answers (1)

Damian
Damian

Reputation: 2852

Database is not specified, you have to add:

Database=myDataBase

to your connection string:

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

Upvotes: 3

Related Questions