C# 如何在C中加载数据库及其表名

C# 如何在C中加载数据库及其表名,c#,sql,visual-studio-2008,C#,Sql,Visual Studio 2008,我正在尝试在Windows窗体中加载数据库名及其表名 我使用此代码获取系统中的服务器名称 private void ServerName() { try { DataTable dt = SmoApplication.EnumAvailableSqlServers(true); if (dt.Rows.Count > 0) {

我正在尝试在Windows窗体中加载数据库名及其表名

我使用此代码获取系统中的服务器名称

private void ServerName()
       {
           try
           {

               DataTable dt = SmoApplication.EnumAvailableSqlServers(true);
               if (dt.Rows.Count > 0)
               {
                   foreach (DataRow dr in dt.Rows)
                   {
                       string serverName = dr[0].ToString();
                       if (!serverName.Contains("\\SQLEXPRESS"))
                       {
                           serverName = serverName + "\\SQLEXPRESS";
                       }
                       comboBox1.Items.Add(serverName);
                   }
                   comboBox1.Items.Add(@".\sqlexpress");
               }
           }
           catch (Exception)
           {
               MessageBox.Show("Problem In Fetching Server Information.");
           }

       }
我使用加载特定服务器的数据库名称

enter code here

private void DBnames()
        {
            server = comboBox1.SelectedItem.ToString();
            database = "master";
            con = new SqlConnection(@"Data Source=" + server + ";Initial Catalog=" + database + ";Integrated Security=True;");
            con.Open();
            da = new SqlDataAdapter("SELECT name FROM sys.databases", con);
            ds = new DataSet();
            da.Fill(ds);
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                comboBox2.Items.Add(ds.Tables[0].Rows[i][0]);
            }
            con.Close();
        }
现在我想加载这个数据库的表名

我使用这个代码

private void TBnames()
        {
            con.Open();
            da = new SqlDataAdapter("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE'",con);
            ds = new DataSet();
            da.Fill(ds);
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                comboBox3.Items.Add(ds.Tables[0].Rows[i][0]);
            }



            con.Close();

        }
但它不起作用。请帮助我获取所选数据库的表

提前谢谢

现在我得到了答案

只需更改TBnames中的代码:

private void TBnames()
            {
                con.Open();
                string s = comboBox2.Text;
               // MessageBox.Show(s);
                cmd = new SqlCommand("Use " + s, con);
                cmd.ExecuteNonQuery();
                da = new SqlDataAdapter("SELECT * FROM sys.tables", con);
                ds = new DataSet();
                da.Fill(ds);
                //dataGridView1.DataSource = ds.Tables[0];
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    comboBox3.Items.Add(ds.Tables[0].Rows[i][0]);
                }
                con.Close();
            }
它正在工作。

使用LINQ可能:

using (var db = new DataClassesDataContext(getConnectionString))
{
    db.Mapping.GetTables();
}

什么不起作用?您是否正在获取错误消息?例外?您是否没有得到预期的结果?它显示了数据库设计。现在我更改了代码,它开始工作了。。