如何在C#中加载DataBase及其表名

人气:771 发布:2022-09-22 标签: sql visual-studio sql-server

问题描述

嗨专家。 我正在尝试在Windows窗体中加载数据库名称及其表名。 我使用此代码获取系统中的服务器名称

Hi experts. I am trying to load the database names and their table names in Windows Forms. 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.

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..

推荐答案

嗨VijayDhas, 您可以通过在sql server中执行此查询来检查 Hi VijayDhas, You can check by executing this query in sql server
if db_id('master') is not null
   --code mine :)
   print 'db exists'
   else
   print 'Not exists'

将输出为 db存在 如果你想要从C#查看它然后参考这个链接 [ ^ ]。你会有一些想法来检查来自C#的数据库的存在。 希望这对你有所帮助。 问候, RK

Will give output as db exists If you want to check it from C# then refer this link[^].You will get some idea to check the existence of database from C#. Hope this helps you a bit. Regards, RK

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

       }

我更改了TBnames()函数中的代码。现在它显示了所有表格。

I have change the code in the TBnames() function. Now it shows all the tables.

984