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

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

最佳答案

检索所有数据库表 您需要从 information_schema.tables 进行查询并将结果绑定(bind)到所需的组合框

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

关于c# - 如何使用 Windows 窗体应用程序检索 ComboBox 中的数据库表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25216492/

相关文章:

c# - mvvm 通用应用程序为什么它没有挂起

php - PIC32 可以发布到 MySQL 数据库吗?

mysql - PowerShell MySQL 转储脚本未正确转储

delphi - 如何在运行时使用另一个过程中的过程在组合框上设置 onChange 事件?

c# - 我如何摆脱这个goto?

c# - SQL 查询输出中的空引用异常

c# - 使用 Entity Framework 功能删除记录与直接使用 DELETE stmt

php - 如何从 Ionic 移动应用程序连接到 mysql?

javascript - 在 JavaScript 中显示 Combobox 项目的文本 onchange()

c# - ComboBox 不允许用户使用空值