C# 不停止使用相同的用户名注册

C# 不停止使用相同的用户名注册,c#,sql,C#,Sql,阻止用户使用相同用户名的完整代码 上面的代码用于阻止人们使用与之相同的用户名 已用于注册 但不幸的是,这并没有阻止他们在现有的网站上注册 用户名,因此它们仍然可以使用相同的用户名。 按钮1的代码\u单击 这是我的提交按钮的代码 try { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString

阻止用户使用相同用户名的完整代码

上面的代码用于阻止人们使用与之相同的用户名 已用于注册 但不幸的是,这并没有阻止他们在现有的网站上注册 用户名,因此它们仍然可以使用相同的用户名。 按钮1的代码\u单击 这是我的提交按钮的代码

    try
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
            con.Open();

            //inserting data to database
            string insertQuery = "insert into [UserDataTable] (UserName,Email,Password,Country) values (@username, @email, @password, @country)";
            SqlCommand com = new SqlCommand(insertQuery, con);
            com.Parameters.AddWithValue("@username", username.Text);
            com.Parameters.AddWithValue("@email", email.Text);
            com.Parameters.AddWithValue("@password", password.Text);
            com.Parameters.AddWithValue("@country", DropDownList1.SelectedItem.ToString());

            com.ExecuteNonQuery();
            Response.Redirect("Manager.aspx");
            Response.Write("You're Now Registered");


            con.Close();
         }
        catch(Exception ex)
        {
            Response.Write("Error:"+ex.ToString());

        }
我的数据库 我创建的数据库

    CREATE TABLE [dbo].[UserDataTable] (
[Id]       INT NULL,
[UserName] NVARCHAR (50) NULL,
[Email]    NCHAR (50)    NULL,
[Password] NCHAR (20)    NULL,
[Country]  NCHAR (15)    NULL     
我的问题: 为什么不阻止他们使用现有的用户名

我是通过剖析我在网上看到的其他代码来写这段代码的,所以如果它看起来很奇怪,我很抱歉这是我第一次使用C

希望这有助于理解我的问题


软件:Visual Studio 2017首先摆脱SQL注入:

 string checkuser = "select * from UserDataTable where UserName=@uname";
然后使用DataTable或DataReader。例如,让我们使用DataReader:

////Firstly,we generally declare the connection as con for easy-recognition 

 SqlConnection con = new SqlConnection;
 con = "connectionstringhere";

 ///then we use the sqlCommand which we generally declare as cmd

 SqlCommand cmd = new SqlCommand(checkuser,con);
 cmd.Parameters.Add("@uname",SqlDbType.Varchar).Value = usernameStringHere

 ///then we use a datareader(note: A datareader is better than DataAdapter regarding performance 

 SqlDataReader dr;
 dr = cmd.ExecuteReader;

 ///Then we check if any row exists in the dataReader after we filter the database 

 If dr.Hasrows
 { 

  ////Wont save data

 }   
上面的代码将完成这项工作

或者你可以简单地使用ExecuteScalar

总结:您有两个问题:

一,。Sql注入-我清楚地解释了如何修复它

二,。使用temp==1而不是temp>0


希望这对您有所帮助

尝试运行该查询并查看您得到的结果。可能你得到的结果与你预期的不同

我建议改变一下:

if (temp == 1)
致:

这将消除用户名已经在数据库中存在两次的情况

另外,正如其他人在评论中提到的,您肯定希望使用参数。以下是转换为使用参数的代码:

string checkuser = "select count(*) from UserDataTable where UserName = @userName";
SqlCommand com = new SqlCommand(checkuser, con);
com.Parameters.Add(new SqlParameter("@userName", username.Text));
int temp = Convert.ToInt32(com.ExecuteScalar());

99.99999999%的几率用户名不是主键

只需运行select,查看是否有多个用户使用输入用户名

快速修复 改变

从…中选择计数*

从…中选择前1名


如果有任何匹配项,它将始终返回1,如果没有,则返回NULL。

了解SQL注入攻击,不要从用户输入构建SQL字符串。使用参数化查询。非常奇怪的代码-假设您使用MSDN sample作为起点,非常不清楚为什么最终使用SQL注入和笨拙的ToInt32而不是强制转换。。。即使您从…开始,它也不起作用。这意味着什么?为什么不阻止他们使用现有用户名?在不知道何时运行第一个代码块的情况下,不可能告诉您答案。它是否真的在Button1_Click事件中运行,以防止重复条目被输入?我写下了答案,并记住OP将来可能会使用datatable:好的,我编辑代码并使用datareader,你现在可以取消否决票了吗?如果你提供了一些可编译的代码,并删除了你要求向你解释计数*意味着你应该将问题作为问题而不是答案的一部分发布…请重新查看答案。。。。我希望你能找到足够好的方法来取消否决票:谢谢你的回复,我编辑了我的原始代码,上面的问题请再看一看,我使用了你的建议,现在我收到了一个错误系统。InvalidOperationException:ExecuteScalar需要一个开放且可用的连接。连接的当前状态为关闭。您需要添加con.Open;在您尝试执行查询之前,请将其添加到您的代码中
if (temp == 1)
if (temp > 0)
string checkuser = "select count(*) from UserDataTable where UserName = @userName";
SqlCommand com = new SqlCommand(checkuser, con);
com.Parameters.Add(new SqlParameter("@userName", username.Text));
int temp = Convert.ToInt32(com.ExecuteScalar());