我的 if 条件不能正常工作

发布于 2024-10-04 02:34:28 字数 661 浏览 3 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

听你说爱我 2024-10-11 02:34:28

您可能在一端或另一端有空格。

尝试更改您的 MessageBox 调用以用引号将字符串括起来,或添加 .Trim 进行比较:

... (reader["ID"].ToString().Trim(), "Doctor") ...

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:

... (reader["ID"].ToString().Trim(), "Doctor") ...
£噩梦荏苒 2024-10-11 02:34:28

您确定字符串“Doctor”没有前导或尾随空格吗?如果将其显示在 MessageBox 中,您将无法真正看到它们。

尝试

MessageBox.Show("-" + reader["ID"] + "-");

看看。

另请参阅: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

MessageBox.Show("-" + reader["ID"] + "-");

to see.

See also: string.Trim

空城仅有旧梦在 2024-10-11 02:34:28

尝试:

reader["ID"].ToString().Trim();

Try:

reader["ID"].ToString().Trim();
り繁华旳梦境 2024-10-11 02:34:28

尝试,注意 Trim() 函数

while (reader.Read()) 
        { 
            MessageBox.Show(reader["ID"].ToString());// its shows Doctor 
                if (string.Equals(reader["ID"].ToString().Trim(), "Doctor"))// why false?! 
                { 

                    //flag = true; 
                    MessageBox.Show("hello"); 
                    str = reader[2].ToString(); 
                    break; 
                } 
        } 

while (reader.Read()) 
        { 
            MessageBox.Show(reader["ID"].ToString());// its shows Doctor 
                if (string.compare(reader["ID"].ToString().Trim(), "Doctor",true)==0)// why false?! 
                { 

                    //flag = true; 
                    MessageBox.Show("hello"); 
                    str = reader[2].ToString(); 
                    break; 
                } 
        } 

try , Notice Trim() function

while (reader.Read()) 
        { 
            MessageBox.Show(reader["ID"].ToString());// its shows Doctor 
                if (string.Equals(reader["ID"].ToString().Trim(), "Doctor"))// why false?! 
                { 

                    //flag = true; 
                    MessageBox.Show("hello"); 
                    str = reader[2].ToString(); 
                    break; 
                } 
        } 

or

while (reader.Read()) 
        { 
            MessageBox.Show(reader["ID"].ToString());// its shows Doctor 
                if (string.compare(reader["ID"].ToString().Trim(), "Doctor",true)==0)// why false?! 
                { 

                    //flag = true; 
                    MessageBox.Show("hello"); 
                    str = reader[2].ToString(); 
                    break; 
                } 
        } 
箜明 2024-10-11 02:34:28

也许存储在数据库中的字符串以空格开头或结尾。

您可能想尝试:

if (String.Equals(reader["ID"].ToString().Trim(), "Doctor")) {
}

或者,更清楚地说:

if (reader["ID"].ToString().Trim() == "Doctor")) {
}

Maybe the string stored in your database begins or ends with whitespace.

You might want to try:

if (String.Equals(reader["ID"].ToString().Trim(), "Doctor")) {
}

Or, more clearly:

if (reader["ID"].ToString().Trim() == "Doctor")) {
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文