将文本框值与数据库值进行比较 [英] Compare textbox value with database value

查看:143
本文介绍了将文本框值与数据库值进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想将textbox(Time)中的值与我的数据库中的值(Time也)进行比较,但不知何故它不会比较该值,它总是会给我MessageBox.Show(您的日期和时间是有效的。);,任何帮助?



  private  < span class =code-keyword> void  onShowClick( object  sender,RoutedEventArgs e){

SqlConnection conn;
SqlCommand cmdSelect;
SqlDataReader阅读器;
string connStr = ConfigurationManager.ConnectionStrings [ ]的ConnectionString。
conn = new SqlConnection(connStr);

string select = 从日程表中选择日期,时间,结束时间;
cmdSelect = new SqlCommand( select ,conn);

尝试 {
conn.Open();
reader = cmdSelect.ExecuteReader();
var valueDate = ;
var valueStart = ;


while (reader.Read()){

valueDate = reader [ 日期]。ToString();
valueStart = reader [ Time]。ToString();

}

if (datePicker1.SelectedDate.ToString()。Equals(valueDate)&& timeText .Text.Equals(valueStart))
{
MessageBox.Show( 你有同样的您的记录中的日期和时间。请选择其他日期和/或时间);
}
else {
MessageBox.Show( 您的日期和时间有效。);
}
conn.Close();
}
catch (Exception ex){
MessageBox.Show(ex.Message);
}


}

解决方案

嗯,是的。 ..可能会,因为它总是比较相同的值 - 从你的数据库读取的最后一个。



你不要试图限制返回哪些记录使用SQL WHERE子句,因此它将返回所有记录。然后循环遍历它们所有检索并丢弃它们已经处理完最后一行。该行中的值以及 valueDate valueStart 左边的那一行作为字符串。



然后你根据用户选择的日期检查一个单一的值,然后相应地报告。



我怀疑你要么想要将测试移动到循环中,或者(更好)使用WHERE子句仅返回匹配的那些值(甚至更好的是那些值的计数)。



BTW:您不是将日期存储在DB中的字符串中吗?因为它看起来像你,这是一个非常非常糟糕的想法,后来会导致大量的问题。 DATETIME字段更有用......


显然,从数据库返回的日期和时间与数据标签和文本框中显示的日期和时间不同。

您可以通过调试来帮助自己,使用Debug.Write方法检查从数据库返回的数据和时间值与用户的值。

请查看如何使用 Debug.Write [ ^ ]

这些差异的可能原因之一可能是数据库中使用的不同日期和时间格式

Hello i want to compare the value in textbox(Time) with the value(Time also) in my database, but somehow it would not compare the value, it would always give me the MessageBox.Show("Your date and time is valid.");, any help?

private void onShowClick(object sender, RoutedEventArgs e) {

           SqlConnection conn;
           SqlCommand cmdSelect;
           SqlDataReader reader;
           string connStr = ConfigurationManager.ConnectionStrings["house"].ConnectionString;
           conn = new SqlConnection(connStr);

           string select = "Select Date, Time, EndTime from Schedule";
           cmdSelect = new SqlCommand(select, conn);

           try {
               conn.Open();
               reader = cmdSelect.ExecuteReader();
               var valueDate = "";
               var valueStart = "";


               while(reader.Read()){

                   valueDate = reader["Date"].ToString();
                   valueStart = reader["Time"].ToString();

               }

               if (datePicker1.SelectedDate.ToString().Equals(valueDate) && timeText.Text.Equals(valueStart))
               {
                      MessageBox.Show("You have a same date and same time in your record(s). Please choose another date and/or time");
               }
               else {
                   MessageBox.Show("Your date and time is valid.");
               }
               conn.Close();
           }
           catch(Exception ex){
               MessageBox.Show(ex.Message);
           }


       }

解决方案

Well, yes...it probably will, because it is always comparing the same value - the last one read from your DB.

You don't try to restrict which records are returned with an SQL WHERE clause, so it will return all records. You then loop through them all retrieving and discarding them util the have processed teh last row. The values from that row and that row alone at left in the valueDate and valueStart as strings.

Then you check that one single value against the user selected date, and report accordingly.

I suspect that you either want to move your test into the loop, or (better) use a WHERE clause to return only those values (or even better yet a count of those values) which do match.

BTW: You aren't storing your dates as strings in the DB are you? Because it looks like you are and that is a very, very poor idea that leads to loads of problems later. A DATETIME field is a lot more useful...


Apparently, the date and time returned from the database are not the same as those presented in the datapicker and the textbox.
You can help yourself by doing a debug, use the Debug.Write method to check the values of the data and time returned from the database against those from the user.
Check this out on how to use Debug.Write [^]
One of the possible reason for these discrepancies could be the different date and time format used in the database.


这篇关于将文本框值与数据库值进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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