当前位置:编程学习 > C#/ASP.NET >>

C# SqlDataAdapter 批量修改数据问题

代码:
string connstr = System.Configuration.ConfigurationSettings.AppSettings["ConnStr"].ToString();
            sqlconn = new SqlConnection(connstr);
            SqlDataAdapter sd = new SqlDataAdapter("select top(1) * from " + tablename, sqlconn);

            //SqlCommandBuilder scb = new SqlCommandBuilder(sd);
            string sql ="update " + tablename + " set FEndTime = @FEndTime,FEndState = @FEndState,FCanReason = @FCanReason,FRC2Ver=@FRC2Ver,FPorcessflag = @FPorcessflag where FOrderNo =@FOrderNo";
            SqlCommand sqlcmd = new SqlCommand(sql,sqlconn);
           
            
            sqlcmd.Parameters.Add("@FCanReason", SqlDbType.NVarChar, 200, "FCanReason");
            sqlcmd.Parameters.Add("@FEndTime", SqlDbType.NVarChar, 20, "FEndTime");
            sqlcmd.Parameters.Add("@FRC2Ver", SqlDbType.NVarChar, 50, "FRC2Ver");
            sqlcmd.Parameters.Add("@FPorcessflag", SqlDbType.Int, 4, "FPorcessflag");
            sqlcmd.Parameters.Add("@FEndState", SqlDbType.Int, 4, "FEndState");
            sqlcmd.Parameters.Add("@FOrderNo", SqlDbType.NVarChar, 50, "FOrderNo");

            sqlcmd.UpdatedRowSource = UpdateRowSource.OutputParameters;


            sd.UpdateCommand = sqlcmd;

            sd.UpdateBatchSize = 100;
          
            try
            {
              int f=  sd.Update(UpdateDataTable); //执行到该行跳出

              int t = f;     // 执行不到这一行
            }
            catch (InvalidOperationException e)
            {
            } --------------------编程问答-------------------- 你Catch(Exception ex),看是什么错误 --------------------编程问答-------------------- 楼主  为什么我的不能实例化Excel.Application对象 ? --------------------编程问答--------------------
引用 2 楼 wenjie11533 的回复:
楼主 为什么我的不能实例化Excel.Application对象 ?



 错了!  郁闷   不好意思 --------------------编程问答--------------------  string sql = "if exists (select FOrderNo from " + tablename + " where FOrderNo =@FOrderNo) update " + tablename + " set FCanReason = @FCanReason,FEndTime = @FEndTime,FRC2Ver=@FRC2Ver,FPorcessflag = @FPorcessflag, FEndState = @FEndState where FOrderNo =@FOrderNo";
            SqlCommand sqlcmd = new SqlCommand(sql,sqlconn);

修改了SQL语句,错误提示有新行,需要insertcommand
上述语句能否实现datatable中的数据在数据表中存在就修改,不存在不修改。 --------------------编程问答-------------------- Catch(Exception ex)看message --------------------编程问答-------------------- 错误提示: datatable中有新的datarow, 需要Insertcommand命令。。 --------------------编程问答-------------------- 错误为:当传递具有新行的 DataRow 集合时,更新要求有效的 InsertCommand
因为只需要更新操作,不需要插入,所以 sql:
string sql = "if exists (select FOrderNo from " + tablename + " where FOrderNo =@FOrderNo) update " + tablename + " set FCanReason = @FCanReason,FEndTime = @FEndTime,FRC2Ver=@FRC2Ver,FPorcessflag = @FPorcessflag, FEndState = @FEndState where FOrderNo =@FOrderNo";
  SqlCommand sqlcmd = new SqlCommand(sql,sqlconn);

试图避免修改时发现新的数据,但是这样的方法还是报错,请高手指教!! --------------------编程问答-------------------- 你只提供了UpdateCommand,并没有提供InsertCommand,
除非你用SqlCommandBuilder scb = new SqlCommandBuilder(sd)才能根据SelectCommand推导出Insert与Update --------------------编程问答-------------------- 可是有Insertcommand 后,datatable有新行后,他会自动插入啊,我datatable中是手动构造的,只保存了需要修改的字段。,如果自动构建insertcommand后,会因为某些字段为空而报错。
我现在只需要修改数据,datatable中和数据库中都有的修改,datatable中有,数据表中没有的,不用修改。

在线等,分不够,加 --------------------编程问答--------------------   DataTable dt = new DataTable();
          DataTable dtUpdateOnly =  dt.GetChanges(DataRowState.Modified);
sd.Update(dtUpdateOnly ); --------------------编程问答-------------------- datatable存储的数据不是有原数据修改得到的,而是修改后的数据添加进去的。只包括修改的字段和主键。
现在把这些修改该的数据批量写到数据库中,数据库有的修改,没有的不修改。 
楼上(ufo20020427)的方法不行。
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,