找回密码
 立即注册

QQ登录

只需一步,快速开始

zheng_hq

银牌会员

151

主题

391

帖子

2227

积分

银牌会员

积分
2227

活字格认证

zheng_hq
银牌会员   /  发表于:2012-4-23 11:20  /   查看:8937  /  回复:12
标准的操作 access 数据库

可以修改,却无法 添加 和 删除  数据

我用vb.net + datagridview 实现了,换成c1flexgrid 却出了问题,到底是哪里错了呢?

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x

12 个回复

倒序浏览
iceman
社区贡献组   /  发表于:2012-4-23 15:30:00
沙发

回复 1# zheng_hq 的帖子

zheng_hq 你好,
关于 C1FlexGrid 与数据库交互,基本分为两步:
更改数据源,重新绑定 C1FlexGrid 数据。

关于删除-对应事件中需要通过 SQL 语句去更新数据库。我没有发现有删除语句。SQL 语句写法建议楼主参考 MSDN 。
回复 使用道具 举报
zheng_hq
银牌会员   /  发表于:2012-4-23 15:52:00
板凳
我绑定的是 access 数据库

private void add()
        {
            OleDbConnection dltConn = new OleDbConnection();
            dltConn.ConnectionString = strCon;
            dltConn.Open();
            string strInsert = "insert into dlt(qh,q1,q2,q3,q4,q5,h1,h2) VALUES  ( ";
            strInsert = strInsert + "" + Convert.ToInt32(textBox1.Text.Trim()) + ",";
            strInsert = strInsert + "" + Convert.ToInt32(textBox2.Text.Trim()) + ",";
            strInsert = strInsert + "" + Convert.ToInt32(textBox3.Text.Trim()) + ",";
            strInsert = strInsert + "" + Convert.ToInt32(textBox4.Text.Trim()) + ",";
            strInsert = strInsert + "" + Convert.ToInt32(textBox5.Text.Trim()) + ",";
            strInsert = strInsert + "" + Convert.ToInt32(textBox6.Text.Trim()) + ",";
            strInsert = strInsert + "" + Convert.ToInt32(textBox7.Text.Trim()) + ",";
            strInsert = strInsert + "" + Convert.ToInt32(textBox8.Text.Trim()) + ",";
            strInsert = strInsert + ")";

            try
            {
                //这里是添加后 号码递增
                textBox1.Text = (Convert.ToInt32(textBox1.Text) + 1).ToString();
                textBox2.Text = "";
                textBox3.Text = "";
                textBox4.Text = "";
                textBox5.Text = "";
                textBox6.Text = "";
                textBox7.Text = "";
                textBox8.Text = "";

                OleDbCommand inst = new OleDbCommand(strInsert, dltConn);
                inst.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show("添加失败,请检查是否有重名或者填写不全");
            }
            dltConn.Close();
            refresh();
        }
回复 使用道具 举报
zheng_hq
银牌会员   /  发表于:2012-4-23 15:52:00
地板
private void Del_Data()
        {
            int i = updateGrid.Rows.Count;
            for (i = 0; i <= updateGrid.Rows.Count - 1; i++)
            {
                if (updateGrid.Rows.Selected)
                {
                    Del_Record_DataBase(updateGrid[i,0].ToString());
                    i = updateGrid.Rows.Count + 1;
                }
            }
        }
        private void Del_Record_DataBase(string id)
        {
            OleDbConnection dltConn = new OleDbConnection(strCon);
            dltConn.Open();
            string strDele = "DELETE FROM dlt WHERE xh= " + id.Trim() + "";
            OleDbCommand dltCommand = new OleDbCommand(strDele, dltConn);
            //从数据库中删除指定记录
            dltCommand.ExecuteNonQuery();
            dltConn.Close();
            //更新datagridview
            binddata();
        }
回复 使用道具 举报
zheng_hq
银牌会员   /  发表于:2012-4-23 15:53:00
5#
private void Button1_Click(object sender, EventArgs e)
        {
            add();
            binddata();
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            modify();
            binddata();
        }

        private void Button3_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定要删除吗?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                Del_Data();
                textBox1.Text = "";
                textBox2.Text = "";
                textBox3.Text = "";
                textBox4.Text = "";
                textBox5.Text = "";
                textBox6.Text = "";
                textBox7.Text = "";
                textBox8.Text = "";
            }
        }
回复 使用道具 举报
zheng_hq
银牌会员   /  发表于:2012-4-23 15:54:00
6#
无法实现  add()添加记录   del_data()删除记录
回复 使用道具 举报
iceman
社区贡献组   /  发表于:2012-4-23 17:34:00
7#

回复 6# zheng_hq 的帖子

zheng_hq 你好,
能否发个完整的 Demo 到论坛上调试?
回复 使用道具 举报
zheng_hq
银牌会员   /  发表于:2012-4-23 22:21:00
8#
在“手动更新”模块,实在是困惑不已

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复 使用道具 举报
iceman
社区贡献组   /  发表于:2012-4-24 10:20:00
9#
zheng_hq 你好,
调好了,请参考 Demo:

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复 使用道具 举报
zheng_hq
银牌会员   /  发表于:2012-4-24 10:31:00
10#
可以实现删除了,但还是无法添加数据

我看了“删除模块“的代码,几乎没看出有什么变化,能告诉我原因吗?
回复 使用道具 举报
12下一页
您需要登录后才可以回帖 登录 | 立即注册
返回顶部