yhkqwkz446 2016-02-24 09:21 采纳率: 0%
浏览 1702

下面通过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条回答

  • threenewbee 2016-02-25 00:18
    关注

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

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

    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题