ExecuteNonQuery()返回受影响的C#的意外行数 [英] ExecuteNonQuery() Returns unexpected number of rows affected C#

查看:54
本文介绍了ExecuteNonQuery()返回受影响的C#的意外行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码

      // SqlCommand query = new SqlCommand("INSERT INTO devis (idProposition, identreprise, tauxHoraire, fraisGenerauxMO, fraisGenerauxPiece, beneficeEtAleas, idStatut, prixUnitaireVenteMO ) VALUES(@idproposition,  @identreprise, @tauxHoraire, @fraisGenerauxMO, @fraisGenerauxPiece, @beneficeEtAleas, 1, @prixUnitaireVenteMO) ", Tools.GetConnection());
     SqlCommand query = new SqlCommand("INSERT INTO devis (idProposition, identreprise, tauxHoraire, fraisGenerauxMO, fraisGenerauxPiece, beneficeEtAleas, idStatut, prixUnitaireVenteMO, alerteEntrepriseEnvoyee,fraisDeplacement ) VALUES(1051,  85, 20, 2, 2, 2.2, 1, 88,0,-1) ", Tools.GetConnection());

    //query.Parameters.AddWithValue("idproposition", this.ID);
    //query.Parameters.AddWithValue("identreprise", competitor.ID);
    //query.Parameters.AddWithValue("tauxHoraire", competitor.CoefTauxHoraire);
    //query.Parameters.AddWithValue("fraisGenerauxMO", competitor.CoefFraisGenerauxMO);
    //query.Parameters.AddWithValue("fraisGenerauxPiece", competitor.CoefFraisGenerauxPiece);
    //query.Parameters.AddWithValue("beneficeEtAleas", competitor.CoefBeneficeEtAleas);
    //query.Parameters.AddWithValue("prixUnitaireVenteMO", Math.Round(competitor.CoefTauxHoraire * competitor.CoefFraisGenerauxMO * competitor.CoefBeneficeEtAleas, 2));
 bool insertOK = (query.ExecuteNonQuery() == 1);
 if (insertOK)
 {
    // DO SOMETHING
 }

insertOk 为假,但在数据库中插入该行以及我指定的所有信息

insertOk is false but in the database the row is inserted with all the information that I specified

我手动重建查询以查看问题是否来自查询.参数,它再次无错误地插入数据库,但insertOk仍然为false!我什至还添加了另外两个不应为空的字段,但两种情况下的活动都是相同的

I rebuilt the query manually to see if the problem came from the query.Parameters, it inserts without error again into the database but insertOk is still false! I even added two other fields which aren't supposed to be null but the activity is the same in both cases

有什么想法吗?

推荐答案

对于UPDATE,INSERT和DELETE语句,返回值是受命令影响的行数.

For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command.

当要插入或更新的表上存在触发器时,返回值包括受插入或更新操作影响的行数以及受一个或多个触发器影响的行数.对于所有其他类型的语句,返回值为-1.如果发生回滚,则返回值为-1.

When a trigger exists on a table being inserted or updated, the return value includes the number of rows affected by both the insert or update operation and the number of rows affected by the trigger or triggers. For all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1.

这篇关于ExecuteNonQuery()返回受影响的C#的意外行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆