Sql Query返回行,但SqlDataReader返回null [英] Sql Query return rows,but SqlDataReader return null

查看:114
本文介绍了Sql Query返回行,但SqlDataReader返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在执行一个返回一组行的Sql Server 2008查询。当我从代码执行相同的查询时,使用SqlDataReader,reader.Read()返回null。任何想法??



查询是一个简单的Select语句,带有两个声明的变量,用于Where子句。



Thnx提前

Hello everyone,

I am executing a Sql Server 2008 query which returns a set of rows. When i execute the same query from code, using SqlDataReader ,reader.Read() returns null. Any ideas??

The query is a simple Select Statement,with two declared variables which are used in Where Clause.

Thnx in advance

推荐答案

Reader.Read 不返回 null - 它不能,返回类型是 bool 这是值类型 ,结果是不可空的。



可能,你的意思是它没有返回任何行,或者 reader 为null并尝试使用它抛出一个空引用异常。



因此,在方法的第一行放置一个断点,然后单步执行代码查看变量,到底是怎么回事。如果你不能从中解决这个问题,我们需要看到产生问题的代码,以及对问题究竟是什么的更好的描述。
Reader.Read does not return null - it can't, the return type is bool which is a value type, and as a result is not nullable.

Probably, what you mean is that it returns no rows, or reader is null and the attempt to use it throws a "null reference" exception.

So, place a breakpoint on the first line of the method, and single step through your code looking at the variables, and what is going on. If you can't work it out fro yourself from that, we would need to see the code that generated the problem, and a better description of exactly what the problem is.


我解决了它,代码还可以,我只需添加一行代码



I solved it,the code was ok,i just addad a line of code

cmd.ExecuteNonQuery(); 




此行之前






before this line

rdr = cmd.ExecuteReader(); 





它有效。我不知道为什么会这样,但现在好了。希望这有助于其他任何人......



and it worked. I dont know why this happend but its ok now. Hope this helps anyone else...


这篇关于Sql Query返回行,但SqlDataReader返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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