如何获取数据库(sql)表名和列在C#中的组合和列表框 [英] how to get database (sql) table names and colums in C# to combo and list box

查看:266
本文介绍了如何获取数据库(sql)表名和列在C#中的组合和列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图玩一个小数据库,并使一个程序,应该连接到一个数据库使用信息,我给它(那部分工作正常)

i'm trying to "play" with a small database and made a program that should connect to a database using info i give it (that part works fine)

登录后的另一部分程序应该连接到数据库并做2件事情:
1.获取所有的表名并将其放入组合框
2.获取表的所有列名选择组合框),并将它们显示在列表框中

after login another part of the program should connect to the database and do 2 things: 1. get all the table names and put it into a combobox 2. get all the column names of a table (choose form the combobox) and display them in a listbox

从该i计划中检查列名称并使用它在表上运行不同的SQL命令

from that i plan to check a column name and use it to run different SQL commands on the tables

我的问题是我无法使它工作

my problem is that i can't make it work

搜索stackoverflow和google的其他示例,我知道我缺少一些小东西,使这个东西不工作,但我不知道什么。

searched other examples from stackoverflow and google and i know i'm missing something small that makes this thing not work, but i don't know what.

请求帮助更多的信息,然后我自己的这个问题

ask for help from people with greater info then my own on this matter

    private void listTables()
    {
        const string query = "SELECT * FROM [dstut].sys.Tables";

        SqlConnection sqlConn = new SqlConnection(lw.ConnectionString);
        sqlConn.Open();

        SqlCommand cmd = new SqlCommand(query, sqlConn);
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        sda.Fill(ds);
        List<string> l = new List<string>();
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            l.Add(dr[0].ToString());
        }
        cbTables.DataSource = l;
    }

    public DataSet GetAllColumns()
    {
        string query = "SELECT name FROM " + TableName;

        SqlConnection sqlConn = new SqlConnection(lw.ConnectionString);
        sqlConn.Open();
        SqlCommand cmd = new SqlCommand(query, sqlConn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            MessageBox.Show(Convert.ToString(dr["Field_1"]));
        }
        return ds;

    }


推荐答案

两个子部分可以得到它工作使用以下方法。而对于最后一部分,你必须去与自己的UI。请记住,您不是第一个创建查询表达式生成器工具的人。所以它值得检查或google的免费/开源查询表达式生成器工具。

The first two sub section can be get it worked using following approach. And for the last part, you have to go with your own UI. Keep in mind, you are not the first person to create Query Expression Builder tool. So its worth checking or google for free/open source Query Expression Builder tools.

使用以下查询返回所有

SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'

您可以执行以下查询以从第一个组合中选择表格返回所有列

You can execute following query to return all columns from selected table from your first combo

SELECT COLUMN_NAME, * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='SelectedValueFromCombo1' ORDER BY ORDINAL_POSITION

这篇关于如何获取数据库(sql)表名和列在C#中的组合和列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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