How to Retrieve tables of a database in a ComboBox

2019-09-06 16:43发布

问题:

This is the code, I used for adding database tables. Its working but I want to show the added tables name in a comboBox. For an example: If I add a schoolName1 into the database, how can I show the schoolName1 in to comboBox ?. Thanks.

private void button1_Click(object sender, EventArgs e)
{

    string myConnectionString= @"DataSource =.\SQLEXPRESS;AttachDbFileName=myconnection.mdf;Integrated Security=true;"

    SqlConnection dbConnection = new SqlConnection(myConnectionString);

    string myCommand = "CREATE TABLE["+textBox1.Text+"] (column1 VARCHAR(10),colunm2 INT)";

    SqlCommand dbConnection = new SqlCommand(myCommand,dbConnection);

    try
       {
          dbConnection.Open();
          dbCommand.ExecuteNonQuery();
       }

    catch{}

          dbConnection.Close();
 }

回答1:

To Retrieve all database tables you need to make query from information_schema.tables and bind results to desired combox

    String strConnection = "Data Source=HP\\SQLEXPRESS;database=your_db;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);
    }

This is my way you can use several ways to retrieve this information and show in combobox reference : link

I have edited and complete your code :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public string myConnectionString = @"DataSource =.\SQLEXPRESS;AttachDbFileName=myconnection.mdf;Integrated Security=true;"

        private void Form1_Load(object sender, EventArgs e)
        {
            FillCombo();
        }

        private void FillCombo()
        {
            SqlConnection dbConnection = new SqlConnection(myConnectionString);
            SqlCommand sqlCmd = new SqlCommand();
            try
            {

                sqlCmd.Connection = dbConnection;
                sqlCmd.CommandType = CommandType.Text;
                sqlCmd.CommandText = "use database_name; SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE';";
                SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);
                DataTable dtRecord = new DataTable();
                sqlDataAdap.Fill(dtRecord);
                comboBox1.DataSource = dtRecord;
                comboBox1.DisplayMember = "TABLE_NAME";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            dbConnection.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection dbConnection = new SqlConnection(myConnectionString);
            string myCommand = "CREATE TABLE["+textBox1.Text+"] (column1 VARCHAR(10),colunm2 INT)";
            SqlCommand dbCommand = new SqlCommand(myCommand, dbConnection);
            try
               {
                  dbConnection.Open();
                  dbCommand.ExecuteNonQuery();
                  FillCombo();
               }

            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            dbConnection.Close();
         }
        }
}