Reputation: 47
I have a combobox
that I would like to use to select the database from a selection available to the user. I have found plenty of information on populating the fields with table values but nothing on making a selection on which .dbo
they can use. I'm guessing the same principle can be used as below...
but I would presume that (database =
) needs to be taken out and replaced some how.
any advice would be appreciated
var connectionString = "server = (local); database = database; integrated security = true;"
string Sql = "select database...";
SqlConnection _con = new SqlConnection(connectionString);
_con.Open();
SqlCommand cmd = new SqlCommand(Sql, conn);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
combobox1.Items.Add(reader[0]);
}
Upvotes: 1
Views: 66
Reputation: 111
You can remove database option from connection string and execute sql query against master.dbo.sysdatabases
.
Complete example:
var connectionString = "server=(local);integrated security=true;"
string sql = "SELECT name FROM master.dbo.sysdatabases";
SqlConnection conn= new SqlConnection(connectionString);
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
combobox1.Items.Add(reader["name"]);
}
Upvotes: 1
Reputation: 39966
You need this query;
string Sql = "SELECT * FROM sys.databases";
Upvotes: 2