2 yhkqwkz446 yhkqwkz446 于 2016.02.24 17:21 提问

下面通过dataset更新数据库,代码哪里有错误,各位大哥,我都纠结一天了(winform)
     //更新数据源的方法
        private int UpdateByDataSet(DataSet ds, string strTblName, string strconn)
        {


           // dgvHead.Columns["pid"].Visible = true;
            //dgvHead.Columns["fNo"].Visible = true;
            //dgvHead.Columns["positation"].Visible = true;

            try
            {

                //dgvHead.AllowUserToAddRows = false;
                SqlConnection cn = new SqlConnection(strconn);
                SqlDataAdapter sda0 = new SqlDataAdapter();
                //sql是要更新的源表中的部分
                string sql = @"select id,
                            worktimeNo as 工序代码,
                            initialCount as 起始针数,
                            seamDistance as 车缝距离, 
                            endCount as 落回针数,
                            worktimeName as 工序名称,
                            samTime as SAM值,
                            target AS 工序指标,
                            equipment as 使用设备,
                            tool as 使用工具,
                            pid from " + strTblName + " where pid=" + id + " and positation='头部' and fno='" + txtfNo.Text + "'";
                SqlCommand cmd1 = new SqlCommand(sql, cn);
                sda0.SelectCommand = cmd1;


                //sda.UpdateCommand = cmd;
                //sda.DeleteCommand = cmd;
                //sda.InsertCommand = cmd;

                //sda1.Fill(ds, strTblName);



                SqlCommandBuilder cb = new SqlCommandBuilder(sda0);
                sda0.Update(ds, strTblName);



                return 0;
            }
            catch (Exception ee)
            {

                MessageBox.Show(ee.ToString());
                return -1;
            }
        }


datagridview初始化
  private void dgvHeadInit(int id)
        {

            string sql1 = @"select id,
                            worktimeNo as 工序代码,
                            initialCount as 起始针数,
                            seamDistance as 车缝距离, 
                            endCount as 落回针数,
                            initialCount+seamDistance+endCount as [每小时指标(件)],
                            worktimeName as 工序名称,
                            samTime as SAM值,
                            target AS 工序指标,
                            equipment as 使用设备,
                            tool as 使用工具,
                            pid 
                            from product_desProductTime 
                            where pid=" + id + " and positation='头部' and fno='"+txtfNo.Text+"'";

            SqlConnection cn = new SqlConnection("server=172.20.50.4;uid=sa;pwd=p4japo020;database=gsd");
            sda1 = new SqlDataAdapter(sql1, cn);
            ds1 = new DataSet();
            sda1.Fill(ds1);
            dt1 = ds1.Tables[0];

            ds1.Tables[0].TableName = "product_desProductTime";




            dgvHead.DataSource = dt1;

            dgvHead.Columns["id"].Visible = false;
                        }
   private void btnSave_Click(object sender, EventArgs e)
     {
                        int flag = 2;
            flag= UpdateByDataSet(ds1, "product_desProductTime",strconn);
     }

我的初衷是通过直接在ui层操作数据(增删改),然后点击保存按钮,数据就会更新到数据库表中;我自己写了一个测试程序,功能完全可以实现,但是移植到项目中就老是不行,求解释,我都纠结一整天了 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!要发狂了

补充一下,数据库表结构:
CREATE TABLE [dbo].product_desProductTime NOT NULL,
[fNo] nvarchar NULL,
[positation] nvarchar NULL,
[worktimeNo] nvarchar NULL,
[worktimeName] nvarchar NULL,
[samTime] decimal NULL,
[target] decimal NULL,
[equipment] nvarchar NULL,
[tool] nvarchar NULL,
[initialCount] [int] NULL,
[seamDistance] [int] NULL,
[endCount] [int] NULL,
[pid] [int] NULL,
CONSTRAINT [PK__product___3213E83F123EB7A3] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

5个回答

caozhy
caozhy   Ds   Rxr 2016.02.25 08:18

贴出来报什么错,或者程序怎么不正确。

strconn
这里连接字符串对不对?

caozhy
caozhy 回复yhkqwkz446: 你是select并没有修改数据,要看到改变,应该用insert delete或者update
接近 2 年之前 回复
yhkqwkz446
yhkqwkz446 程序没错,就是对数据库没影响
接近 2 年之前 回复
yhkqwkz446
yhkqwkz446 程序没错,就是对数据库没影响
接近 2 年之前 回复
caozhy
caozhy   Ds   Rxr 2016.02.25 09:05

http://blog.sina.com.cn/s/blog_7850ccad01014p76.html

注意,修改的时候不需要select,否则你重新加载更新了datagridview,那么你的修改就白费了。

yhkqwkz446
yhkqwkz446 回复caozhy: 我现在知道问题出在哪里 就是那个dgvHead_CellLeave和dgvHead_CellValidated事件 我控制不了,所以有的时候能更新成功,有的时候更新失败
接近 2 年之前 回复
yhkqwkz446
yhkqwkz446 回复caozhy: 我现在知道问题出在哪里 就是那个dgvHead_CellLeave和dgvHead_CellValidated事件 我控制不了,所以有的时候能更新成功,有的时候更新失败
接近 2 年之前 回复
yhkqwkz446
yhkqwkz446 回复caozhy: 主表更新没问题,主要是子表(也就是那个datagridview中的数据更新到数据库有问题)
接近 2 年之前 回复
yhkqwkz446
yhkqwkz446 回复caozhy: 主表更新没问题,主要是子表(也就是那个datagridview中的数据更新到数据库有问题)
接近 2 年之前 回复
yhkqwkz446
yhkqwkz446 回复caozhy: 最新代码在下面,但是我发现 有的时候能更新成功,有时候就不行
接近 2 年之前 回复
caozhy
caozhy 回复yhkqwkz446: 我知道啊
接近 2 年之前 回复
yhkqwkz446
yhkqwkz446 我现在的想法是通过一个保存按钮来保存用户对UI层的所有操作(增删改),不是分别写好几个按钮实现的那种
接近 2 年之前 回复
yhkqwkz446
yhkqwkz446   2016.02.25 09:51
   //保存按钮:更新操作(主表和子表)
        private void btnSave_Click(object sender, EventArgs e)
        {
            //int id = 0;
            //保存到数据库之前先判断一下主表是否有重复记录
            string fno = txtfNo.Text.Trim();
            string fname = txtPname.Text.Trim();
            int orderqty = Convert.ToInt32(txtOrderQty.Text);
            double orderc = Convert.ToDouble(txtOrderCoefficient.Text);
            Image pic = pbchef.Image;
            //主表的修改
            string sql1 = "select count(1) from desProductAttach where fno='" + fno + "' and fno!='" + args["pNo"].ToString() + "' and fname='" + fname + "' and orderqty=" + orderqty + " and orderCoefficient=" + orderc + "";
            int count = Convert.ToInt32(SqlHelp.GetValue(CommandType.Text, sql1));
            if (count != 0)
            {
                MessageBox.Show("数据库中已经存在重复记录,请修改后重新录入!");
                return;
            }
            string sql2 = @"update desProductAttach set fno='" + fno + "',fname='" + fname + "',orderqty=" + orderqty + ",orderCoefficient=" + orderc + " where id="+id+"";
            SqlHelp.ExecuteNonQuery(CommandType.Text,sql2);

           //子表的更新(通过数据源更新)
            //int flag = 2;
            //flag= UpdateByDataSet(ds1, "product_desProductTime",strconn);
            if (ds1.HasChanges())
            {
                SqlCommandBuilder cb = new SqlCommandBuilder(sda1);
                sda1.Update(ds1.Tables[0]);
                ds1.AcceptChanges();
                dgvHead.Update();
            }

        }
   private void dgvHead_CellLeave(object sender, DataGridViewCellEventArgs e)
        {
            //第一次加载时候,dgvHead.Rows[e.RowIndex].Cells[e.ColumnIndex].Value默认是第一行的id值
            if (dgvHead.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == null)
            {
                tempCellValue = null;
                return;
            }
            tempCellValue = dgvHead.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
        }
   //失去焦点 dgv
        private void dgvHead_CellValidated(object sender, DataGridViewCellEventArgs e)
        {
            string curValue = dgvHead.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
            if (tempCellValue==curValue)
            {
                return;
            }
            else
            {
                if (e.ColumnIndex==3)
                {
                    string workTimeNo = dgvHead.CurrentRow.Cells[3].Value.ToString();
                    string sql2 = @"select count(*) from desProductTime where workTimeNo='"+workTimeNo+"'";
                    cn = new SqlConnection(strconn);
                    cmd = new SqlCommand(sql2,cn);
                    cn.Open();
                    int x = (int)(cmd.ExecuteScalar());
                    cn.Close();
                    if (x!=0)
                    {
                        cn = new SqlConnection(strconn);
                        string sql3 = @"select * from desProductTime where workTimeNo='"+workTimeNo+"'";
                        cmd = new SqlCommand(sql3,cn);
                        cn.Open();

                        SqlDataReader sdr = cmd.ExecuteReader();
                        while (sdr.Read())
                        {

                            dgvHead.Rows[e.RowIndex].Cells[1].Value = txtfNo.Text;
                            dgvHead.Rows[e.RowIndex].Cells[2].Value = "头部";
                            dgvHead.Rows[e.RowIndex].Cells[4].Value = sdr["initialCount"].ToString();
                            dgvHead.Rows[e.RowIndex].Cells[5].Value = sdr["seamDistance"].ToString();
                            dgvHead.Rows[e.RowIndex].Cells[6].Value = sdr["endCount"].ToString();
                            dgvHead.Rows[e.RowIndex].Cells[7].Value = (int)dgvHead.Rows[e.RowIndex].Cells[4].Value + (int)dgvHead.Rows[e.RowIndex].Cells[5].Value + (int)(dgvHead.Rows[e.RowIndex].Cells[6].Value);
                            dgvHead.Rows[e.RowIndex].Cells[8].Value = sdr["worktimeName"].ToString();
                            dgvHead.Rows[e.RowIndex].Cells[9].Value = sdr["samTime"].ToString();
                            dgvHead.Rows[e.RowIndex].Cells[10].Value = sdr["target"].ToString();
                            dgvHead.Rows[e.RowIndex].Cells[11].Value = sdr["equipment"].ToString();
                            dgvHead.Rows[e.RowIndex].Cells[12].Value = sdr["tool"].ToString();
                            dgvHead.Rows[e.RowIndex].Cells[13].Value = id;


                        }
                        cn.Close();
                    }
                    else
                    {
                        MessageBox.Show("工序代码输入有误!");
                        return;
                    }


                }
                else
                {

                    dgvHead.Rows[e.RowIndex].Cells[7].Value = (int)dgvHead.Rows[e.RowIndex].Cells[4].Value + (int)dgvHead.Rows[e.RowIndex].Cells[5].Value + (int)(dgvHead.Rows[e.RowIndex].Cells[6].Value);
                }
            }
        }


yhkqwkz446
yhkqwkz446   2016.02.25 09:53

相关界面截图:图片说明

Royal_lr
Royal_lr   Ds   Rxr 2016.02.26 20:30

修改的问题吧,,,,

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