How to get list of all database from sql server in a combobox using c#.net

前端 未结 6 1038
北荒
北荒 2020-12-13 16:14

I am entering the source name userid and password through the textbox and want the database list should be listed on the combo box so that all the four options sourcename, u

6条回答
  •  情话喂你
    2020-12-13 16:49

        using (var connection = new System.Data.SqlClient.SqlConnection("ConnectionString"))
        {
            connection.Open();
            var command = new System.Data.SqlClient.SqlCommand();
            command.Connection = connection;
            command.CommandType = CommandType.Text;
            command.CommandText = "SELECT name FROM master.sys.databases";
    
            var adapter = new System.Data.SqlClient.SqlDataAdapter(command);
            var dataset = new DataSet();
            adapter.Fill(dataset);
            DataTable dtDatabases = dataset.Tables[0];
        }
    

提交回复
热议问题