比较datareader返回的值 [英] Compare values returned by datareader

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

问题描述

以下代码无法评估返回值:

输出默认标签名称。



 SqlDataReader dr = cmd.ExecuteReader(); 

while (dr.Read())
{
..
sex = dr [< span class =code-string> sex]。ToString();
..
}
if (性别== < span class =code-string>男性)
{
Label1.Text = 标记;
}
其他 如果(性别== 女性
{
Label1.Text = Marry;
}





怎么做?

解决方案

请记住,这只会与您的查询返回的数据库中的最后一个匹配行进行比较:我们不知道您的查询是什么。

因此,如果您有十几行男性然后一行带有Yes please,那么比较只会将Yes please与Male和Female进行比较,并且两者都失败。因此,您的标签的当前内容将不会更改。



因此,为您知道存在问题的按键添加启动器的默认值:

  if (性别==  男性
{
Label1.Text = 标记;
}
其他 如果(性别== 女性
{
Label1.Text = Marry;
}
else
{
Label1.Text = 未知 +性别;
}



从那里你应该有一个更好的主意 - 但检查你的查询只返回一行...还记得字符串比较是大小写敏感...


The following code doesn't working to evaluate the returned value:
Outputs the default Label Name.

SqlDataReader dr = cmd.ExecuteReader();
                
while (dr.Read())
{
..
sex = dr["sex"].ToString();
..
}
if (sex == "Male")
{
   Label1.Text = "Mark";
}
else if(sex == "Female")
{
   Label1.Text = "Marry";
}



How to do this?

解决方案

Remember that this will be comparing only against the last matching row in the database that your query returns: and we don't know what your query is.
So if you have a dozen rows with "Male" and then one row with "Yes please" then the comparison will only compare "Yes please" against "Male" and "Female" and fail both. As a result, the current content of your label will not be changed.

So add a default for starters that keys you know that there is a problem:

if (sex == "Male")
{
   Label1.Text = "Mark";
}
else if(sex == "Female")
{
   Label1.Text = "Marry";
}
else
{
   Label1.Text = "Unknown " + sex;
}


And from there you should get a better idea - but check your query returns only one row as well... Also remember that string comparisons are case sensitive...


这篇关于比较datareader返回的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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