如何使用Windows窗体应用程序检索ComboBox中的数据库表? [英] How to Retrieve tables of a database in a ComboBox using an windows form application?

查看:101
本文介绍了如何使用Windows窗体应用程序检索ComboBox中的数据库表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是代码,我用于添加数据库表。它可以正常工作,但是我想在 comboBox 中显示添加的表名。
例如:如果我将 schoolName1 添加到数据库中,如何在 schoolName1 中显示到 comboBox 吗?

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


推荐答案

检索所有数据库表
您需要从information_schema.tables中进行查询并将结果绑定到所需的combox

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

这是我的方式
您可以使用几种方式来检索信息并显示在组合框
中:链接

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

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

这篇关于如何使用Windows窗体应用程序检索ComboBox中的数据库表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆