想开发一个,能根据所选择的数据库名称,来统计该数据内各表的行数有多少的小窗体,如下图那样;代码如下,之前数据库名称都是文本框,但改为下拉菜单后,就不知怎么写了,请指导一下,谢谢
```c#
private void button1_Click(object sender, EventArgs e)
{
String connectionString = String.Format("server={0};uid={1};pwd={2};", textBoxHost.Text, textBoxUser.Text, textBoxPassword.Text);
SqlConnection sqlconn = new SqlConnection(connectionString);
sqlconn.Open();
SqlString = "select name from sys.databases where name NOT IN ('master', 'tempdb', 'model', 'msdb')";
//加载数据并显示
//string query = "SELECT name from sys.databases where name NOT IN ('master', 'tempdb', 'model', 'msdb')";
try
{
MessageBox.Show("数据库连接成功!");
//查询条件和SqlConnection连接
SqlCommand cmd = new SqlCommand(SqlString, sqlconn);
//数据适配器
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
//DataTable存储数据
DataTable dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;
}
catch (Exception ex)
{
MessageBox.Show("数据库连接失败,请填写准确连接参数");
return;
}
finally
{
conn.Close();
}
}
private void button2_Click(object sender, EventArgs e)
{
String connectionString = String.Format("server={0};uid={1};pwd={2};", textBoxHost.Text, textBoxUser.Text, textBoxPassword.Text);
SqlConnection sqlconn = new SqlConnection(connectionString);
sqlconn.Open();
SqlString = "SELECT a.name, b.rows FROM sysobjects AS a INNER JOIN sysindexes AS b ON a.id = b.id WHERE (a.type = 'u') AND (b.indid IN (0, 1)) ORDER BY b.rows desc";
//加载数据并显示
try
{
//查询条件和SqlConnection连接
SqlCommand cmd = new SqlCommand(SqlString, sqlconn);
//数据适配器
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
//DataTable存储数据
DataTable dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;
}
catch
{ }
finally
{
conn.Close();
}
}
```