2 yhkqwkz446 yhkqwkz446 于 2016.02.23 11:24 提问

关于datagridview:通过更新数据源,来更新数据库
dgvHead初始化代码:
  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 使用工具 
                        from product_desProductTime 
                        where  positation='头部' and pid=" + id + "";
            //string sql1 = "select worktimeNo as 工序代码,initialCount as 起始针数,seamDistance as 车缝距离, endCount as 落回针数,worktimeName as 工序名称,samTime as SAM值,target AS 工序指标,equipment as 使用设备,tool as 使用工具 from product_desProductTime where 1=2";
            //DataTable dt1 = SqlHelp.GetDataTable(CommandType.Text, sql1);
            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);

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



            dgvHead.DataSource = ds1.Tables[0];
            dgvHead.Columns["id"].Visible = false;
            // dgv.DataSource = dt1;//分别绑定数据源
            DataRow dr1 = ds1.Tables[0].NewRow();
            ds1.Tables[0].Rows.Add(dr1);


        }
    //更新数据集的方法
        private int UpdateByDataSet(DataSet ds, string strTblName, string strconn)
        {
            try
            {
                SqlConnection cn = new SqlConnection(strconn);
                SqlDataAdapter sda = new SqlDataAdapter();
                SqlCommand cmd = new SqlCommand(@"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 使用工具 
                        from " + strTblName+" where  positation='头部' and pid=" + id + "", cn);
                sda.SelectCommand = cmd;
                SqlCommandBuilder cb = new SqlCommandBuilder(sda);
                sda.Update(ds1,strTblName);
                return 0;
            }
            catch (Exception ee)
            {

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

                保存按钮:
                UpdateByDataSet(ds1, "product_desProductTime",strconn);

问题:为什么我点击保存按钮之后,数据库中的数据并没有修改,但是却增加了一条新的全是null的记录???求大神解答,多谢

1个回答

bdmh
bdmh   Ds   Rxr 2016.02.23 11:32

ds1.Tables[0].Rows.Add(dr1);你这不是增加了一个空行吗

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