How to Retrieve tables of a database in a ComboBox using an windows form application? [closed]

三世轮回 提交于 2019-12-11 14:34:48

问题


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


来源:https://stackoverflow.com/questions/25216492/how-to-retrieve-tables-of-a-database-in-a-combobox-using-an-windows-form-applica

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!