C#:未在预期的oracle表中反映的更新查询值

时间:2017-10-06 14:41:29

标签: c# .net oracle

我尝试使用c#更新oracle表中的某些记录。我没有收到错误,但是当我查看我想要更新的记录时,我发现记录仍然具有相同的旧值。为了查看每个查询是否成功执行,我使用了这一行rowid= cmd.ExecuteNonQuery();,它通过查询19583返回受影响的行数。我得到的数字是表格中的行数。

[OperationContract]

  public int pushData(string CustomObjects)
  {

      List<CustomObject> myDeserializedObjList = (List<CustomObject>)Newtonsoft.Json.JsonConvert.DeserializeObject(CustomObjects, 
      typeof(List<CustomObject>));
      string constr = "my connection string";
      int rowid = 0;
      using(OracleConnection con = new OracleConnection(constr)){

      con.Open();
      OracleCommand cmd = new OracleCommand();
      cmd.Connection = con;
      cmd.CommandType = CommandType.Text;

       foreach (CustomObject element in myDeserializedObjList)
       {

          int num = element.num;

          string mydate = element.mydate;

          long num2 = element.num2;

          string user = element.user;


          string sqlStatement= "UPDATE CustomObjectS SET  num = num, 
          mydate=mydate, num2=num2, user=user  WHERE num = num";
          OracleTransaction myTrans;

          // Start a local transaction
          myTrans = con.BeginTransaction();
          // Assign transaction object for a pending local transaction
          cmd.Transaction = myTrans;
          cmd.CommandText=sqlStatement;
          cmd.Parameters.Add(new OracleParameter("num", num));
          cmd.Parameters.Add(new OracleParameter("user", user));
          cmd.Parameters.Add(new OracleParameter("num2", num2));
          cmd.Parameters.Add(new OracleParameter("mydate", OracleDbType.Date));
          cmd.Parameters[3].Value = mydate;


                  rowid= cmd.ExecuteNonQuery();

                  myTrans.Commit();



          }


     }


      return rowid;

  }

1 个答案:

答案 0 :(得分:2)

您似乎正在使用其当前值更新行。尝试将:添加到查询中的参数名称:

UPDATE CustomObjectS SET  num = :num, 
          mydate=:mydate, num2=:num2, user=:user  WHERE num = :num

Using Oracle parameters