我的 if 条件不能正常工作
我正在 ADO.NET 上使用 C# 工作,我成功连接了数据库,一切都很好,所以我将检查放在 while(reader.Read) 中以检查特定值,但每次尝试时条件都给出 false,
string qry = "select * from LoginTB";
reader = db.select_data(qry);
while (reader.Read())
{
MessageBox.Show(reader["ID"].ToString());// its shows Doctor
if (string.Equals(reader["ID"].ToString(), "Doctor"))// why false?!
{
//flag = true;
MessageBox.Show("hello");
str = reader[2].ToString();
break;
}
}
am working in C# on ADO.NET, i connected with my database successfully and everything is fine so i put check in side while(reader.Read) to check specefic value but the condition is giving false everytime i try,
string qry = "select * from LoginTB";
reader = db.select_data(qry);
while (reader.Read())
{
MessageBox.Show(reader["ID"].ToString());// its shows Doctor
if (string.Equals(reader["ID"].ToString(), "Doctor"))// why false?!
{
//flag = true;
MessageBox.Show("hello");
str = reader[2].ToString();
break;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可能在一端或另一端有空格。
尝试更改您的 MessageBox 调用以用引号将字符串括起来,或添加 .Trim 进行比较:
You probably have whitespace at one end or another.
Try changing your MessageBox call to surround the string with quotation marks, or add a .Trim to the comparison:
您确定字符串“Doctor”没有前导或尾随空格吗?如果将其显示在 MessageBox 中,您将无法真正看到它们。
尝试
看看。
另请参阅:string.Trim
Are you sure the string "Doctor" doesn't have leading or trailing spaces? You can't really see them if you display it in a MessageBox.
Try
to see.
See also: string.Trim
尝试:
Try:
尝试,注意 Trim() 函数
或
try , Notice Trim() function
or
也许存储在数据库中的字符串以空格开头或结尾。
您可能想尝试:
或者,更清楚地说:
Maybe the string stored in your database begins or ends with whitespace.
You might want to try:
Or, more clearly: