Home > Database > Mysql Tutorial > 怎样将一个数据库里的所有数据表的名字绑定到ComboBox中

怎样将一个数据库里的所有数据表的名字绑定到ComboBox中

WBOY
Release: 2016-06-07 15:11:32
Original
1615 people have browsed it

C#高手群:207364916期待您的加入 private void Form1_Load(object sender, EventArgs e) { //声明一个SQLconnection对象 SqlConnection sql = new SqlConnection(server=WINDOWS-UBBCFE0;database=VCD;uid=sa;pwd=123); //声明一个SQLcommand对象 SqlComman

 C#高手群:207364916期待您的加入

private void Form1_Load(object sender, EventArgs e)
        { 

            //声明一个SQLconnection对象
            SqlConnection sql = new SqlConnection("server=WINDOWS-UBBCFE0;database=VCD;uid=sa;pwd=123");

            //声明一个SQLcommand对象
            SqlCommand sqlcmd = new SqlCommand("SELECT Name FROM SysObjects Where XType='U' ORDER BY Name", sql);

            //用DataAdapter声明一个对象
            SqlDataAdapter sda = new SqlDataAdapter();

           //执行查询语句
            sda.SelectCommand = sqlcmd;

          //创建一个数据集对象
            DataSet ds = new DataSet();

           //调用DataAdapter对象的Fill()方法来填充数据集
            sda.Fill(ds, "Name");

           //绑定combobox的数据集
            comboBox1.DataSource = ds.Tables["Name"];

          //选定combobox显示的成员及将Name这一列显示在combobox中
            comboBox1.DisplayMember = "Name"; 
        }

执行后的结果如图:怎样将一个数据库里的所有数据表的名字绑定到ComboBox中

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template