简体   繁体   English

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

[英]How to load the DataBase and their table names in C#

I am trying to load the database names and their table names in Windows Forms. 我正在尝试在Windows窗体中加载数据库名称及其表名称。

I use this code to Get the server names in the System 我使用此代码获取系统中的服务器名称

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.");
           }

       }

I use to load the Database Names of that particular Server. 我用来加载该特定服务器的数据库名称。

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

Now i want load the table names for this Data base 现在我要加载该数据库的表名

I use this code 我用这个代码

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();

        }

But it is not working. 但这是行不通的。 Please help me to get the tables of the selected DB. 请帮助我获取所选数据库的表。

Thanks in advance.. 提前致谢..

Now i got the answer.. 现在我得到了答案。

Just change the code in TBnames: 只需更改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();
            }

It's working.. 在工作

Use LINQ maybe :) 也许使用LINQ :)

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

http://msdn.microsoft.com/en-us/library/bb655883(v=vs.90).aspx http://msdn.microsoft.com/zh-CN/library/bb655883(v=vs.90).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM