比较datareader返回的值

编程入门 行业动态 更新时间:2024-10-26 06:36:36
本文介绍了比较datareader返回的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

以下代码无法评估返回值: 输出默认标签名称。

SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { .. sex = dr [ sex]。ToString(); .. } if (性别== 男性) { 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返回的值

本文发布于:2023-05-31 10:04:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/389528.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:datareader

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!