如何使用Windows窗体应用程序检索ComboBox中的数据库表?

时间:2014-08-09 07:57:59

标签: c# mysql combobox data-retrieval

这是我用于添加数据库表的代码。它工作但我想在comboBox中显示添加的表名。 例如:如果我在数据库中添加schoolName1,如何在schoolName1中显示comboBox?感谢。

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 个答案:

答案 0 :(得分:3)

检索所有数据库表 您需要从information_schema.tables进行查询并将结果绑定到所需的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);
    }

这是我的方式 您可以使用多种方法来检索此信息并在组合框中显示 参考:link

我已编辑并完成您的代码:

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