ExecuteNonQuery始终返回零.我可以在代码中使用这个0值进行验证吗? [英] ExecuteNonQuery always returns zero. Can i use this 0 value into my code for validation?

查看:148
本文介绍了ExecuteNonQuery始终返回零.我可以在代码中使用这个0值进行验证吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下c#代码在dba_users表中创建一个oracle用户,其中我正在使用oledbcommand和ExecuteNonQuery.在dba_users表中已成功创建用户,但是ExecuteNonQuery始终将值重新调谐为"0"

I am creating a oracle user in dba_users table by using the below c# code where i am using oledbcommand and ExecuteNonQuery. User is being successfully created in the dba_users table but ExecuteNonQuery is always retun value as "0"

所以我正在用(IsUserCreated == 0)在我的代码中进行验证.我的编码正确吗?

So i am doing validation in my code as (IsUserCreated==0). Am i correct with my coding here?

int IsUserCreated= oleCreateUserCommands.ExecuteNonQuery();

if(IsUserCreated==0)
{
//TBD code
Response.write("User Created Successfully");
}

else
{
//TBD Code
Response.write("User creation failed with some error");
}

推荐答案

正如Marc所说,您不能依赖返回值.根据我的经验,在不同的数据库和语句类型之间,返回值实际上是不一致的或可移植的,对于非DML,成功的返回值可能是-1或0,对于DML,返回的值为0、1或更大.根据他对SET NOCOUNT ON的评论,Oracle不支持它,它是SQL Server的功能.

As Marc said, you can't rely on the return value. The return value is actually not consistent or portable, across different databases and statement types you may see -1 or 0 for success for non-DML, and 0, 1 or greater for DML, in my experience. Per his comment about SET NOCOUNT ON, Oracle doesn't support that, its a SQL Server feature.

顺便说一句,对于CREATE USER语句,尽管我很少使用OleDb,但我总是看到-1(我开发了几种桌面数据库工具,并且做了很多跟踪工作).我很惊讶您看到0,应该仔细检查.

Incidentally, for a CREATE USER statement, I always see -1 (I develop several desktop database tools and I've done a lot of tracing) though I don't use OleDb much. I am surprised you see 0, you should double check.

无论如何,您必须使用异常来处理ExecuteNonQuery和ExecuteScalar及其同级的错误情况.否则就无法编写健壮的代码.缺乏例外意味着成功.至于返回码,除了DML之外,它对于验证确实没有用.您如何编写一个可以接受-1、0或1或N为有效值的通用算法?我知道发出可能的DML并需要将行数返回给用户时,才进行检查.

Regardless, you must use exceptions to handle error cases for ExecuteNonQuery and ExecuteScalar and its siblings. It is not possible to write robust code otherwise. The lack of exception implies success. As far as the return code, it is really useless for validation, except in DML. How do you write a generic algorithm that can accept -1, 0 or 1, or N as valid? I simply check it when I know I issue a possible DML, and need to return the row count to the user.

  1. 您的代码应放在using块中(ADO中的所有IDisposable类型通常应放在using语句中)
  2. 您应该尝试/抓住或者至少尝试/最终

如果您不喜欢重复自己,则将ExecuteNonQuery包装在您自己的函数中,该函数将处理异常并返回布尔值true/false.在某些情况下,我喜欢为连接或阅读器类编写扩展方法.

If you don't like repeating yourself, then wrap ExecuteNonQuery in your own function that will handle exception and return a bool true/false. In certain cases, I like to write extension methods for the connection or reader classes.

这篇关于ExecuteNonQuery始终返回零.我可以在代码中使用这个0值进行验证吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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