如何使用windows窗体应用程序在组合框中检索数据库的表

本文关键字:检索 数据库 组合 windows 何使用 窗体 应用程序 | 更新日期: 2023-09-27 18:17:18

这是我用来添加数据库表的代码。它工作,但我想在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();
 }

如何使用windows窗体应用程序在组合框中检索数据库的表

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