从访问中获取组合框值

时间:2013-09-12 04:50:11

标签: c# winforms ms-access

我有一个winform项目,access filedatabase。 在我的某个表单中,我希望用户从company中选择combobox

(公共场所列表在访问表中)

如何将company name列设置为组合框下拉值?

2 个答案:

答案 0 :(得分:0)

试试这个

string qr1 = "select companyname from table1";
        SqlCommand cmd1 = new SqlCommand(qr1, con);
        con.Open();
        SqlDataReader dr1 = cmd1.ExecuteReader();
        cmbcat.Items.Clear();
        while (dr1.Read())
        {
            cmbcat.Items.Add(dr1[0].ToString());

        }
        con.Close();

答案 1 :(得分:-1)

到尼米

这是oledbcommand吗?

我的解决方案是......

public void make_cbDispatch()           
{     
        string ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;"
                                    + "Data Source=I:\\Projects\\project.accdb;"
                                    + "Persist Security Info=False;";

        string qr1 = "SELECT DISTINCT object "
                                    + "FROM tList "
                                    + "ORDER BY object ";

        OleDbConnection con = new OleDbConnection(ConnectionString);
        OleDbCommand cmd1 = new OleDbCommand(qr1, con);
        con.Open();
         OleDbDataReader dr1 = cmd1.ExecuteReader();
        cbDispatch.Items.Clear();
        while (dr1.Read())
        {
            cbDispatch.Items.Add(dr1[0].ToString());

        }
        con.Close();

}