C#-检查空的数据读取器 [英] C# - Check for empty datareaders

查看:95
本文介绍了C#-检查空的数据读取器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在检查空的数据读取器时遇到问题.我正在将Visual Studio 2012与SQL Server Compact 4.0一起使用. Compact中不支持HasRows属性,因此我不能使用它.

这就是我现在所拥有的.

Hi guys,

I am running into issues with checking for empty datareaders. I am using Visual Studio 2012 with SQL Server Compact 4.0. The HasRows property is not supported in Compact, so I can''t use that.

This is what I have right now.

if ( ( (ac_key_number_dr.Read()) && (cc_key_number_dr.Read()) && (key_number_dr.Read()) && (hybrid_key_number_dr.Read()) && (transmission_key_number_dr.Read()) ) == false )
            {
                loadDefault();
            }



我正在检查所有5个数据读取器是否为空.

我的预期目标是,如果所有5个参数都为空,则调用此函数"loadDefault()".否则,如果其中一位读者有内容,则会跳过此if语句.

我没有收到任何错误消息,但是现在的输出是,此if语句返回true,并且无论所有内容是否为空或其中一个读者是否有内容,该函数都将被调用.我想念什么?



I am checking for all 5 of my datareaders as to whether or not they are empty.

My intended target is that if all 5 are empty to call this function "loadDefault()". Otherwise, if one of the readers has content, this if statement will be skipped.

I''m not getting any error message, but the output right now is that this if statement returns true and this function gets called no matter if all are empty or if one of the readers has content. What am I missing?

Thanks in advance!

推荐答案

假设Read()成功,则Read()返回TRUE,那么您就需要按以下方式更改逻辑.
如果任何Read()成功,则不会调用loadDefault().



Assuming Read() returns TRUE if Read() succeeds then you want change your logic as below.
If any Read() succeeds loadDefault() will not get called.



if ( ( (ac_key_number_dr.Read()) || (cc_key_number_dr.Read()) || (key_number_dr.Read()) || (hybrid_key_number_dr.Read()) || (transmission_key_number_dr.Read()) ) == false )
            {
                loadDefault();
            }


您的逻辑有一个小缺陷:
读取如果有数据,则返回true,因此
Your logic has a small flaw:
Read returns true if it has data, so
a.Read() && b.Read()


当且仅当a和b都具有数据时,才为true.
相反,


is true if and only if a and b both have data.
Conversely,

(a.Read() && b.Read()) == false


如果a或b中的任何一个不包含数据,或者两个都不包含
,则为true 如果只想在没有数据的情况下执行语句,那么您想要


if true if either of a or b does not contain data, or if both do not
If you want your statement executed only when neither have data, then you want

!a.Read() && !b.Read()

只有当它们都为空时,才为真.

因此,您要将其编码为:

Which is true only when they are both empty.

So you want to code it as:

if ( !ac_key_number_dr.Read() && !cc_key_number_dr.Read() && !key_number_dr.Read() && !hybrid_key_number_dr.Read() && !transmission_key_number_dr.Read())
            {
                loadDefault();
            }


这篇关于C#-检查空的数据读取器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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