[英]Check if user already exists in MySql Database with C#
I have the following code and I would like the code to query the database: 我有以下代码,我想查询数据库的代码:
MySqlConnection connection = new MySqlConnection(Connection);
MySqlCommand cmd;
connection.Open();
try
{
cmd = connection.CreateCommand();
cmd.CommandText = ("INSERT INTO edata (username) VALUES ('" + this.NewUserName.Text +"') ;");
MySqlDataReader myReader;
myReader = cmd.ExecuteReader();
loading.Show();
MessageBox.Show("Your Account Has Been Created!!");
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
If the clients input (NewUserName.Text) is the same as the username in the database there should be an error message, otherwise there will be duplicate usernames. 如果客户端输入的(NewUserName.Text)与数据库中的用户名相同,则将出现错误消息,否则将出现重复的用户名。 How can I do this? 我怎样才能做到这一点?
I have already got the connection as a string. 我已经以字符串形式获得了连接。
Thank you in advance!!! 先感谢您!!!
PS Im a student learning C# and MySql, I am grateful for any help and advice! PS我是一名学习C#和MySql的学生,非常感谢您的帮助和建议!
You can create a Unique Constraint on the username field in the database, this will throw an exception if the user already exists. 您可以在数据库的用户名字段中创建唯一约束,如果用户已经存在,这将引发异常。
Alternatively, you do a manual select from the table and see if there is a user with the same name before insertion. 或者,您可以从表中进行手动选择,然后在插入之前查看是否有同名用户。
您可以在适当的列( doc )上创建唯一索引。
CREATE UNIQUE INDEX uq_username ON edata (username);
are you using the membership class? 您在使用会员等级吗?
MembershipCreateStatus createStatus;
MembershipUser newUser = Membership.CreateUser(txtEmail.Text, "password", txtEmail.Text,"What was your first school?","idk", true, out createStatus);
switch (createStatus)
{
case MembershipCreateStatus.Success:
//do your actions here
case MembershipCreateStatus.DuplicateUserName:
CreateAccountResults.Text = "There already exists a user with this username.";
break;
case MembershipCreateStatus.DuplicateEmail:
CreateAccountResults.Text = "There already exists a user with this email address.";
break;
case MembershipCreateStatus.InvalidEmail:
CreateAccountResults.Text = "There email address you provided in invalid.";
break;
case MembershipCreateStatus.InvalidAnswer:
CreateAccountResults.Text = "There security answer was invalid.";
break;
case MembershipCreateStatus.InvalidPassword:
CreateAccountResults.Text = "The password you provided is invalid. It must be seven characters long and have at least one non-alphanumeric character.";
break;
default:
CreateAccountResults.Text = "There was an unknown error; the user account was NOT created.";
break;
}
If you're not using that, then simply create a method that reads the database and call it before the code you have above: 如果您不使用它,则只需创建一个读取数据库的方法,然后在上面的代码之前调用它:
string doesuserexist = ReadDatabase("Select Email from Patients where Email=@emailaddress").TrimEnd();
if (checkemail != "")
{
CreateAccountResults.Text = "User Email Already Exists";
return;
}
You have to write your own ReadDatabase method, but from the code posted above, you obviously know how, just replace the insert sql statement with select. 您必须编写自己的ReadDatabase方法,但是从上面发布的代码中,您显然知道如何做,只需将select替换为insert sql语句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.