问题描述
- 关于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的记录???求大神解答,多谢
解决方案
ds1.Tables[0].Rows.Add(dr1);你这不是增加了一个空行吗
时间: 2025-01-30 18:14:29