yhkqwkz446 2016-02-23 03:24 采纳率: 0%
浏览 1502

关于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 移动开发领域优质创作者 2016-02-23 03:32
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致