简体   繁体   English

检查sql databse中是否存在记录返回错误结果

[英]Checking for existence of a record in sql databse returns wrong results

I'm working on my project in Asp.net (C#), I have one textbox to get a string from the user (by button click), And like all the strings that i get from the user - (username, email, etc), I check if the input string is exists in the database (SQL), if it exists, an error message is shown. 我正在Asp.net(C#)中的项目上工作,我有一个文本框可以从用户那里得到一个字符串(通过单击按钮),就像我从用户那里得到的所有字符串一样-(用户名,电子邮件等) ),我检查数据库(SQL)中是否存在输入字符串,如果存在,则会显示错误消息。

I use this code in .cs Code behind file: 我在文件后面的.cs代码中使用此代码:

string cn = cnTextBox.Value;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("SELECT [CN] FROM [dbo].[MyTable] WHERE CN=@CN");   
cmd.Parameters.AddWithValue("@CN", cn);
cmd.Connection = con;
SqlDataReader dr = cmd.ExecuteReader();
bool theSame = false;
if (dr.HasRows())
    while (dr.Read())
    {            
        string value = dr["CN"].ToString();
        if (cn == value)
            theSame = true;
    }

if (theSame)
    errorMessage.Style["display"] = "block";
else
    errorMessage.Style["display"] = "none";
if (!theSame)
    // code to insert the string to a new record in the database, works fine.

As you can see i use WHERE clause inside a SELECT (parameterized) query, in order to verify if the string is already exists in the database. 如您所见,我在SELECT (参数化)查询中使用WHERE子句,以验证字符串是否已存在于数据库中。

Note: The "CN" column in the database is of type varchar(50) . 注意:数据库中的“ CN”列的类型为varchar(50)

The problem is, (never hppened before, and it is really not the first time i do that), if i have the string "xxxxx" in a record in the database (in the column CN) and the input string that i want to insert is "xxxxx n " (not the same length) or "xxxxn" (not the same chars), the above check returns that this input is already exists in the database and the error message is shown. 问题是(如果从未在数据库中(CN列中)的记录中包含字符串“ xxxxx”和我想要输入的字符串(从来没有过,这不是我第一次来)如果insert是“ xxxxx n ”(长度不相同)或“ xxxxn”(字符不相同),则上述检查返回该输入已经存在于数据库中,并显示错误消息。 And even more strange? 甚至更奇怪? the string is inserted to the database after all. 毕竟该字符串已插入数据库。

Note: It happens over and over again when i test my project using Internet Explorer 11, but never happened when i used another bowser like Firefox. 注意:当我使用Internet Explorer 11测试我的项目时,它会一遍又一遍地发生,但是当我使用另一个类似Firefox的Bowser时却从未发生过。

Is something wrong? 有什么事吗 Any suggestions? 有什么建议么?

Thanks for help! 感谢帮助!

Too long for a comment. 评论太久了。 Are you sure you are looking at the same database that you application is using? 您确定要查看应用程序使用的同一数据库吗?

Unrelated tips: 不相关的提示:

  1. SqlConnection , SqlCommand and SqlDataReader are all IDisposable so each should be in a using block. SqlConnectionSqlCommandSqlDataReader都是IDisposable因此每个都应该在using块中。
  2. You may want to stop using AddWithValue . 您可能要停止使用AddWithValue
  3. You can pass the connection to the constructor of the SqlCommand . 您可以将连接传递给SqlCommand的构造函数。
  4. Rather than dr["CN"].ToString(); 而不是dr["CN"].ToString(); prefer dr.GetString(0); 更喜欢dr.GetString(0);
  5. SQL string comparisons are case insensitive by default. 默认情况下,SQL字符串比较不区分大小写。 C# string equality are case sensitive. C#字符串相等区分大小写。 SQL might find the record and your C# code could dismiss it as being different. SQL可能会找到该记录,而您的C#代码可能会认为它与众不同。
  6. Any question including the phrase " [doing X] returns wrong results" misses the point that computers do exactly what you ask them to, so unless this is a freakish bug which millions of other users have not seen over decades of usage; 包括短语“ [做X]会返回错误的结果”的任何问题都遗漏了计算机完全按照您的要求进行操作,因此,除非这是一个怪异的错误,几十年来,数百万其他用户还没有看到过这个错误。 then you need to realise it is returning the right results for what it has been asked. 那么您需要意识到它正在根据要求返回正确的结果。 This shift in perception, along with use of the debugger is likely to help you diagnose the problem. 这种观念上的转变以及调试器的使用可能会帮助您诊断问题。

trim your value been passed. 修剪您的价值被传递。 may be extra space gets added that make it differ like you said 'xxxx' and 'xxxxn', n may be extra space. 可能会添加额外的空间,使它与您所说的“ xxxx”和“ xxxxn”不同,n可能是额外的空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM