Click here to Skip to main content
15,886,693 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
plz can u help me by giving the c# code for displaying names of the tables i hv created in my sql database...
Posted

All you need to do is issue an SQL query:
SQL
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES

So a basic way is:
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES", con))
        {
        using (SqlDataReader reader = com.ExecuteReader())
            {
            myComboBox.Items.Clear();
            while (reader.Read())
                {
                myComboBox.Items.Add((string) reader["TABLE_NAME"]);
                }
            }
        }
    }
 
Share this answer
 
Comments
[no name] 11-Mar-13 6:24am    
5!
SQL
SELECT * FROM INFORMATION_SCHEMA.TABLES where TABLE_TYPE='BASE TABLE'



or


SQL
USE DBName
GO
SELECT * FROM sys.Tables
GO




Use this query to get all the table name from a database
 
Share this answer
 
v2
Hi Try this one,

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace ConsoleApplication1
{
    class Program
    {
//        Try the following

    //For MySQL:

    //SELECT TABLE_NAME 
    //FROM INFORMATION_SCHEMA.TABLES
    //WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA='dbName'

    //For MS SQL:

    //SELECT TABLE_NAME 
    //FROM INFORMATION_SCHEMA.TABLES
    //WHERE TABLE_TYPE = 'BASE TABLE' 
        static void Main(string[] args)
        {
            SqlConnection con = new SqlConnection("data source=Your server name;database=database name;uid=sa;pwd=your sql server password");
            con.Open();
            SqlCommand cmd = new SqlCommand("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'", con);
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows == true)
            {
                while (dr.Read())
                {
                    Console.WriteLine("Table Name : " + dr[0]);
                }
            }
            if (!dr.IsClosed)
            {
                dr.Close();
            }
            if (con != null)
            {
                con.Close();
            }
            Console.ReadLine();
        }
    }
}


regards
sarva
 
Share this answer
 
Hi professional

this code will help you

        con.Open();
// type='U' is defining type of table system type or user type
        SqlCommand cmd = new SqlCommand("Use Your_table_name select name from sysobjects where type='U'"), con);
        SqlDataReader dr = cmd.ExecuteReader();
        // lstboxcol.Items.Clear();
        while (dr.Read())
        {

            combobox.Items.Add(dr["Column_Name"].ToString());

        }
        con.Close();


happy to help
 
Share this answer
 
v2
Comments
mayuri koul 11-Mar-13 6:12am    
well thank u all for ur answers...:)
i have used the following code..is it correct?..plz help..
private void Form1_Load(object sender, EventArgs e)
{
String strConnection = "Data Source=HP\\SQLEXPRESS;database=MK;Integrated Security=true";

SqlConnection con = new SqlConnection(strConnection);
try
{

con.Open();

SqlCommand sqlCmd = new SqlCommand();

sqlCmd.Connection = con;
sqlCmd.CommandType = CommandType.Text;
sqlCmd.CommandText = "Select table_name from information_schema.tables";

SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);

DataTable dtRecord = new DataTable();
sqlDataAdap.Fill(dtRecord);
comboBox1.DataSource = dtRecord;
comboBox1.DisplayMember = "TABLE_NAME";
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Shubh Agrahari 11-Mar-13 6:14am    
what results it giving.?
any exception or error?
mayuri koul 11-Mar-13 6:18am    
well..i hv tried it and it is giving the results i.e. showing the names of tables...if psb can you help me in like when i click on any table name from combo box the table contents r shown in data grid view...
Shubh Agrahari 11-Mar-13 6:25am    
table content mean column name or all data of columns...?have any procedure..?
C#
private void Form1_Load(object sender, EventArgs e)
        {
            String strConnection = "Data Source=HP\\SQLEXPRESS;database=MK;Integrated Security=true";

            SqlConnection con = new SqlConnection(strConnection);
            try
            {

                con.Open();

                SqlCommand sqlCmd = new SqlCommand();

                sqlCmd.Connection = con;
                sqlCmd.CommandType = CommandType.Text;
                sqlCmd.CommandText = "Select table_name from information_schema.tables";

                SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);

                DataTable dtRecord = new DataTable();
                sqlDataAdap.Fill(dtRecord);
                comboBox1.DataSource = dtRecord;
                comboBox1.DisplayMember = "TABLE_NAME";
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
 
Share this answer
 
v2
Comments
Shubh Agrahari 11-Mar-13 6:26am    
good to attempt yourself and get succeed..

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