2 wushiming216 wushiming216 于 2014.11.24 21:11 提问

C#winform数据库绑定问题

我想在comboBox1里读取access里的数据,想实现数据库改变时,comboBox1也作出相应改变。就是控件与数据库绑定,实现实时刷新?用哪一个指令呀??请帮帮忙,谢谢啦!

5个回答

caozhy
caozhy   Ds   Rxr 2014.11.24 21:36
lzp_lrp
lzp_lrp   Ds   Rxr 2014.11.24 22:28

可以参考以下代码
SqlConnection con = new SqlConnection(conn);
con.Open();
SqlCommand cmd = new SqlCommand("select oper_cod, oper_nam from dept",con);

SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
con.Close();
comboBox1.DataSource = dt;
comboBox1.DisplayMember = "dept_nam";
comboBox1.ValueMember = "dept_cod";

如果要实时刷新,可以用timer事件定时来刷新

wushiming216
wushiming216 你好,谢谢你的回答,我原来是用的timer事件进行刷新的,不过因为我编的是小程序,别人说不要用timer。叫我用binding的指令,我找了好久不会用阿。有没有相应的指令呀?谢谢?
大约 3 年之前 回复
caozhy
caozhy   Ds   Rxr 2014.11.24 22:32

private void Form1_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(conn);
con.Open();
SqlCommand cmd = new SqlCommand("select * from department",con);

SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
con.Close();
comboBox1.DataSource = dt;
comboBox1.DisplayMember = "department";
comboBox1.ValueMember = "departmentId";

    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (comboBox1.SelectedIndex>-1)
        {
            DataRowView drv = (DataRowView)comboBox1.SelectedItem;
            int id = Convert.ToInt32(drv.Row["departmentId"].ToString());
            SqlConnection con = new SqlConnection(conn);
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from major where departmentId='" + id + "'", con);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);             
            DataTable dt = new DataTable();
            sda.Fill(dt);
            comboBox2.DataSource = dt;
            comboBox2.DisplayMember = "major";
            comboBox2.ValueMember = "majorId";
        }
    }
wushiming216
wushiming216 回复wushiming216: 你好,我把修改之后的代码贴出来了,能不能帮我看一下?谢谢
大约 3 年之前 回复
wushiming216
wushiming216 是的,我改了一下,怎么把我的代码贴上去啊?我粘贴之后发送不出去啊
大约 3 年之前 回复
caozhy
caozhy 是要修改下,这是针对sql server的。
大约 3 年之前 回复
wushiming216
wushiming216 你好,谢谢你的回答,我根据你的改了一下,运行错误,能不能帮我看看呀?谢谢
大约 3 年之前 回复
wushiming216
wushiming216   2014.11.25 10:08

private void Form1_Load(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection( @"provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\000000\Desktop\测试\xianshi(timer太多)\1.mdb");
con.Open();

        OleDbDataAdapter adr = new OleDbDataAdapter("select * from 电主轴状态", con);
        DataTable dt = new DataTable();
        adr.Fill(dt);
        comboBox1.DataSource = dt;
        comboBox1.DisplayMember = "电主轴运行状态";
        comboBox1.ValueMember = "记录时间";

    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (comboBox1.SelectedIndex > -1)
        {

            DataRowView drv = (DataRowView)comboBox1.SelectedItem;
            string id = drv.Row["记录时间"].ToString();
            OleDbConnection con = new OleDbConnection( @"provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\000000\Desktop\测试\xianshi(timer太多)\1.mdb");
            con.Open();
            OleDbCommand cmd = new OleDbCommand("select * from 环境状态 where 记录时间=" + id + "", con);
            OleDbDataAdapter sda = new OleDbDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            comboBox2.DataSource = dt;
            comboBox2.DisplayMember = "环境状态判定";
            comboBox2.ValueMember = "记录时间";
        }
caozhy
caozhy   Ds   Rxr 2014.11.25 10:09

access 2007以上使用ace驱动而不是oledb

http://blog.163.com/qyzdmb@126/blog/static/135942386201111223039641/

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!