用访问表名称填充组合框

本文关键字:组合 填充 访问表 | 更新日期: 2023-09-27 18:15:06

我有一个数据表,我能够从我的访问数据库填充没有问题,但我想在其中添加一个步骤:

    private void button_Open_Click(object sender, EventArgs e)
    {
        var open = new OpenFileDialog
                    {
                        InitialDirectory = "c:''",
                        Filter = @"Access Files (*.mdb)|*.mdb|All files (*.*)|*.*",
                        FilterIndex = 0,
                        RestoreDirectory = true,
                        Multiselect = false
                    };
        open.ShowDialog();
        if (string.IsNullOrEmpty(open.FileName)) return; 
        try
        {
            var con = new OleDbConnection();
            con.ConnectionString = "Provider= microsoft.jet.oledb.4.0; data source = " + open.FileName;
            con.Open();
            var dt = new DataTable();
            var da = new OleDbDataAdapter("select * from tblCustomerAccount", con);
            da.Fill(dt);
            dataGridView_AccessDatabase.DataSource = dt.DefaultView;
            con.Close();
        }
        catch (OleDbException ex)
        {
            //get the error message if connection failed
            MessageBox.Show("Error in connection ..." + ex.Message);
        }

    }

我想在那里添加一个用表名填充的组合框,然后,在选择组合框时,填充数据表。

如何用表名填充组合框?

谢谢!

用访问表名称填充组合框

//

string[] restrictions = new string[4];
restrictions[3] = "Table";    
con.Open();
DataTable tabls=con.GetSchema("Tables",restrictions);

返回一个数据表,其中一列表示表名

你可以将这个数据表绑定到combobox,并将datamemebr设置为TABLE_NAME