如何检查值是否已存在

时间:2016-04-24 18:23:42

标签: c# sql

我试图找到很多时间来检查价值是否存在而我找不到它,我需要检查两次:

  
      
  1. 在注册
  2.   
  3. 在登录
  4. 中   

这是我的代码块:

SqlConnection c = new SqlConnection(str);
        SqlCommand cmdUsername = new SqlCommand("SELECT 1 FROM Users WHERE UserName = @userName;", c);
        cmdUsername.Parameters.AddWithValue("userName", userName);
        cmdUsername.CommandType = System.Data.CommandType.Text;
        SqlCommand cmdEmail = new SqlCommand("SELECT 1 FROM Users WHERE Email = @email;", c);
        cmdUsername.Parameters.AddWithValue("email", email);

        c.Open();
        nameExists = (int)cmdUsername.ExecuteScalar();
        emailExists = (int)cmdEmail.ExecuteScalar();
        c.Close();

当我输入电子邮件时,它标记了

emailExists = (int)cmdEmail.ExecuteScalar();

在登录中一切正常。

请帮帮我!谢谢大家。

3 个答案:

答案 0 :(得分:1)

假设您要防止重复的用户名或电子邮件,您必须执行以下操作。 1.将ID列设置为INT,并将其从列属性设置为Identity列。 2.将您的电子邮件或用户名设置为主键,以防止电子邮件或用户名重复,最佳做法是将电子邮件列作为主键,其中有很多用户具有相同名称但具有唯一电子邮件的情况。 FYI

希望有所帮助!

要检查用户名或电子邮件是否已存在,请执行以下操作!

SqlCommand check_User_Name = new SqlCommand("SELECT COUNT(*) FROM [Table] WHERE ([user] = @user || [email] = @email)" , c);
check_User_Name.Parameters.AddWithValue("@user", txtBox_UserName.Text);
check_User_Name.Parameters.AddWithValue("@email", txtBox_Email.Text);
int UserExist = (int)check_User_Name.ExecuteScalar();

if(UserExist > 0)
{
   //Username exist
}
else
{
   //Username doesn't exist.
}

答案 1 :(得分:0)

使用您的代码,稍微优化一下。

  • 只需拨打一次数据库
  • 即使发生异常,也始终关闭连接
  • 未找到时返回false或发生na异常,true otherwis

如果这是登录检查,您还可以添加密码检查。

public bool ValidData(string username, string email, string connectionString)
{
    var c = new SqlConnection(connectionString);
    var cmdUsername = new SqlCommand("SELECT COUNT(*) FROM Users WHERE UserName = @userName AND email = @email;", c);
    cmdUsername.Parameters.AddWithValue("userName", username);
    cmdUsername.CommandType = System.Data.CommandType.Text;
    cmdUsername.Parameters.AddWithValue("email", email);

    c.Open();
    try
    {
        return (int) cmdUsername.ExecuteScalar() > 0;
    }
    catch (Exception ex)
    {
        //log exception

        return false;
    }
    finally
    {
        c.Close();
    }
}

如果您只需要用户名或电子邮件更改:

    var cmdUsername = new SqlCommand("SELECT COUNT(*) FROM Users WHERE UserName = @userName AND email = @email;", c);

为:

    var cmdUsername = new SqlCommand("SELECT COUNT(*) FROM Users WHERE UserName = @userName OR email = @email;", c);

答案 2 :(得分:-1)

试试这个 -

string myConnection=str;//this is your connection string
string userName="";
string email="";
string parameteruserName = "";//your parameter should goes here
string parameterEmail = "";//your parameter should goes here
try
{
    SqlDataReader myReader = null;
    SqlCommand    myCommand = new SqlCommand(SELECT COUNT(*) as count FROM  Users WHERE UserName =" +parameteruserName+" or  Email ="+parameterEmail 
   )  "+  ";", 
                                             myConnection);
    myReader = myCommand.ExecuteReader();
    while(myReader.Read())
    {
      userName=  myReader["count"].ToString();

    }
myReader.close();
myConnection.Close();
}
catch (Exception e)
{
    Console.WriteLine(e.ToString());
}


if(Convert.ToInt32(userName)>0){
//your user name is exists
}