为什么executeNonQuery为insert返回1,为存储过程和触发器返回-1 [英] Why executeNonQuery returns 1 for insert and -1 for stored procedure and trigger

查看:50
本文介绍了为什么executeNonQuery为insert返回1,为存储过程和触发器返回-1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请给我回答这个,我想使用返回值。

解决方案

它没有。与文档中所述的一样,返回值是命令为UPDATE,INSERT或DELETE语句时受影响的行数(因此它可以是0或1,不一定是1所有其他命令的-1和-1。



如果要从存储过程中获取返回值,请使用输出参数并将其类型设置为returnvalue(请参阅 MSDN )。



您也可以使用输出参数来检索任何其他信息。通常,最重要的性能参数是数据库引擎可以快速定位要检索的数据的速度,但只要这很快,使用输出参数而不是输出单行数据会更快(因为它消除了需要)构建服务器返回客户端应用程序的自描述模式。



为什么直接从应用程序调用触发器?如果可以,请完全避免使用触发器。如果你必须,至少只使用它们来实现副作用 - 永远不要直接从客户端应用程序调用它们。


是的,它偶尔会返回-1 - 这个Codeproject文章解释了如何重现问题和解决方案

解决方案:ExecuteNonQuery()总是返回-1调用存储过程 [ ^

Plese give me answer of this,i want to use the return value.

解决方案

It doesn't. Like it says in the documentation, the return value is the number of rows affected when the command is an UPDATE, INSERT or DELETE statement (so it can be 0 or 1, not necessarily 1), and -1 for all other commands.

If you want to get the return value from a stored procedure, use an output parameter and set its type to returnvalue (see MSDN).

You may also use output parameters to retrieve any other information. Usually the most important performance parameter will be how quickly the database engine can locate the data you want to retrieve, but provided that this is quick, using output parameters instead of outputting a single row of data is a bit faster (because it eliminates the need to construct a self-describing schema that the server returns to the client application).

Why are you calling triggers directly from your application? If you can, avoid using triggers at all. And if you must, at least only use them to implement side-effects - never invoke them directly from client applications.


Yes, it occasionally DOES return -1 - this Codeproject article explains how to reproduce the problem and the solution
Solution: ExecuteNonQuery() always returning -1 when calling a Stored Procedure[^]


这篇关于为什么executeNonQuery为insert返回1,为存储过程和触发器返回-1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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