1

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]);
}

2 Answers 2

2

You need this query;

string Sql = "SELECT * FROM sys.databases";
Sign up to request clarification or add additional context in comments.

3 Comments

So its more down to what SQL query i send then? sorry the example didn't work, but that's more likely my end I Guess.
@JamesBurnieMcburnie What is your database? Isn't it SQL Server?
It is, sorry turns out its actually further up in my code and this isn't being executed at the right point causing it not to populate before it is shown to the User. Working now. Thank you
1

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"]);

        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.