C# 使用 SQL Command 对象,如何检查结果集是否为空?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/722462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using the SQL Command object, how can you check to see if the result set is empty?
提问by EverTheLearner
Using the ExecuteScalar method in the SQL Command object, how can you check to see if the result set is empty? I am using ASP.net, C#, and MS SQL 2008. Right now when I run the following code the Response.Write returns a 0when the resultset is empty. But I would like to differentiate between 0and empty resultsets because there are actual 0values in my database.
使用SQL Command对象中的ExecuteScalar方法,如何检查结果集是否为空?我正在使用 ASP.net、C# 和 MS SQL 2008。现在,当我运行以下代码时,当结果集为空时,Response.Write 返回0。但我想区分0和空结果集,因为我的数据库中有实际的0值。
Here is the current code behind:
这是当前的代码:
cmd = new SqlCommand("usp_test", cn);
cmd.CommandType = CommandType.StoredProcedure;
cn.Open();
TestOuput = Convert.ToInt32(cmd.ExecuteScalar());
cn.Close();
Response.Write(TestOutput);
Thank you.
谢谢你。
采纳答案by Kon
Check out the definition of ExecuteScalar. It returns an Object, which will have a null reference if the result set is empty.
查看ExecuteScalar的定义。它返回一个对象,如果结果集为空,它将有一个空引用。
The reason you are seeing zero is that Convert.ToInt32
returns a zero when given null
. You need to check the return value from ExecuteScalar
before you convert it to an int
.
您看到零的原因是Convert.ToInt32
在给定时返回零null
。你需要检查从返回值ExecuteScalar
将其转换为一个之前int
。
回答by Daniel Brückner
DbCommand.ExecuteScalar() returns the first column of the first row, or null if the result is empty. Your problem is caused by Convert.ToInt32() because it returns 0 for null.
DbCommand.ExecuteScalar() 返回第一行的第一列,如果结果为空,则返回 null。您的问题是由 Convert.ToInt32() 引起的,因为它为 null 返回 0。
You have to check the value returned by ExecuteScalar() for null and only call Convert.ToInt32() if it is not null.
您必须检查 ExecuteScalar() 返回的值是否为空,如果它不为空,则仅调用 Convert.ToInt32()。
Object result = command.ExecuteScalar();
if (result != null)
{
Int32 value = Convert.ToInt32(result);
}
else
{
// Handle the empty result set case
}
回答by Canavar
As you can see in hereyou can check if the result is null :
Return Value Type: System.Object The first column of the first row in the result set, or a null reference (Nothing in Visual Basic) if the result set is empty.
返回值 类型:System.Object 结果集中第一行的第一列,如果结果集为空,则为空引用(在 Visual Basic 中为 Nothing)。
回答by Daniel
Execute scalar will return the first column of the first row in the result set. If there are no results it should return null.
执行标量将返回结果集中第一行的第一列。如果没有结果,它应该返回 null。
My guess is that your stored procedure is returning a count and not the dataset, which is why you are seeing a 0.
我的猜测是您的存储过程返回的是计数而不是数据集,这就是您看到 0 的原因。
回答by bdukes
ExecuteScalar
returns null
if the result set is empty (as @fallen888, et al. have said). The reason you are seeing zero is that Convert.ToInt32
returns a zero when given null
. You need to check the return value from ExecuteScalar
before you convert it to an int
.
ExecuteScalar
null
如果结果集为空则返回(如@fallen888等人所说)。您看到零的原因是Convert.ToInt32
在给定时返回零null
。你需要检查从返回值ExecuteScalar
将其转换为一个之前int
。
回答by Sumon Banerjee
cmd = new SqlCommand("usp_test", cn);
cmd.CommandType = CommandType.StoredProcedure;
cn.Open();
Object outPut=cmd.ExecuteScalar();
if(outPut!=null)
TestOuput = Convert.ToInt32(outPut);
else
//Return Empty Set
cn.Close();
Response.Write(TestOutput);