Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ExecuteNonQuery()返回受影响的意外行数#_C#_Sql_Sql Server - Fatal编程技术网

C# ExecuteNonQuery()返回受影响的意外行数#

C# ExecuteNonQuery()返回受影响的意外行数#,c#,sql,sql-server,C#,Sql,Sql Server,这是我的密码 // SqlCommand query = new SqlCommand("INSERT INTO devis (idProposition, identreprise, tauxHoraire, fraisGenerauxMO, fraisGenerauxPiece, beneficeEtAleas, idStatut, prixUnitaireVenteMO ) VALUES(@idproposition, @identreprise, @tauxHoraire,

这是我的密码

      // 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
为false,但在数据库中,该行插入了我指定的所有信息

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


有什么想法吗?

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


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

ExecuteNonQuery
它声明返回受影响的行数。。这是不正确的,因为客户端无法知道受影响的行数。文档错误地假设引擎将报告受影响的行数,但这取决于会话状态。不要编写假定NOCOUNT始终打开的代码。如果需要知道受影响的行数,请使用。依赖于
@@ROWCOUNT
SET NOCOUNT
状态,在许多情况下,从一个或另一个角度来看,值是不正确的。

方法返回

对连接执行Transact-SQL语句并返回 受影响的行数

由于您的
query.ExecuteNonQuery()
返回
2
,因此
2==1
返回
false
太明显了

您的查询将被删除

bool insertOK = (2 == 1);

.

尝试使用
SqlDataAdapter
,以便:

SqlDataAdapter Adabter = new SqlDataAdapter();
 Adabter.InsertCommand = new SqlCommand("INSERT INTO devis values(@idProposition, @identreprise), Tools.GetConnection());

 Adabter.InsertCommand.Parameters.Add("@idproposition",SqlDbType.Int).Value = yorID;
 Adabter.InsertCommand.Parameters.Add("@identreprise", SqlDbType.Int).Value = yorID;

 Tools.OpenConnection();
 int result = Adabter.InsertCommand.ExecuteNonQuery();
 Tools.CloseConnection();

if( result  > 0 )
{
                MessageBox.Show("Information Added");
}else
{
                  MessageBox.Show("Error");
}

那么
ExecuteNonQuery()
返回什么值呢?0? 2.100?数据库中没有影响的行这是来自MSDN的文字复制粘贴。啊,我在表上有一个触发器,可以将一行插入另一个表,这就是为什么感谢您的帮助。这是来自MSDN的文字复制,解释了不匹配的可能原因。@Mikebrian您接受了从MSDN复制的答案吗?真的吗?:)正是这个答案让我找到了解决方案,我想我只是经历了一个显而易见的日子是的,我现在把它改为bool insertOK=(query.ExecuteNonQuery()>0)@mike Bryant如果返回size System.INT32,即使失败,也会返回2。
SqlDataAdapter Adabter = new SqlDataAdapter();
 Adabter.InsertCommand = new SqlCommand("INSERT INTO devis values(@idProposition, @identreprise), Tools.GetConnection());

 Adabter.InsertCommand.Parameters.Add("@idproposition",SqlDbType.Int).Value = yorID;
 Adabter.InsertCommand.Parameters.Add("@identreprise", SqlDbType.Int).Value = yorID;

 Tools.OpenConnection();
 int result = Adabter.InsertCommand.ExecuteNonQuery();
 Tools.CloseConnection();

if( result  > 0 )
{
                MessageBox.Show("Information Added");
}else
{
                  MessageBox.Show("Error");
}