Click here to Skip to main content
15,910,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
C#
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.

C#
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

C#
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..
Posted
Updated 12-Dec-13 0:57am
v2

Hi VijayDhas,

You can check by executing this query in sql server
SQL
if db_id('master') is not null
   --code mine :)
   print 'db exists'
   else
   print 'Not exists'

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
 
Share this answer
 
v2
C#
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();

       }


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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900