[英]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: 不相关的提示:
SqlConnection
, SqlCommand
and SqlDataReader
are all IDisposable
so each should be in a using
block. SqlConnection
, SqlCommand
和SqlDataReader
都是IDisposable
因此每个都应该在using
块中。 SqlCommand
. SqlCommand
的构造函数。 dr["CN"].ToString();
dr["CN"].ToString();
prefer dr.GetString(0);
dr.GetString(0);
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.