检查数据库中是否已存在电子邮件

时间:2014-02-15 18:26:01

标签: c# mysql

我想检查数据库中是否已存在电子邮件地址,一开始,我想提一下,来自互联网的NO代码不起作用。所以请快速帮助和理解作为编程问题我很绿。这是关于电话的基本信息等。

 string constring = "datasource=localhost;port=3306;username=admin;password=admin";

string query = "select * from test.user where email='" + emailtextbox.Text + "';";

MySqlConnection conDatabase = new MySqlConnection(constring);
MySqlCommand cmddatabase = new MySqlCommand(query, conDatabase);

电子邮件称为“电子邮件”的列。

2 个答案:

答案 0 :(得分:1)

解决方案1:您可以Trim用户输入的EmailID然后进行比较。试试这个:

string query = "select count(*) from test.user where email = @email";

MySqlConnection conDatabase = new MySqlConnection(constring);

MySqlCommand cmddatabase = new MySqlCommand(query, conDatabase);
cmddatabase.Parameters.AddWithValue("@email",emailtextbox.Text.Trim());

conDatabase.Open();
int count = Convert.ToInt32(cmddatabase.ExecuteScalar());

if(count > 0)
{
    // yes emailid exists already
}

答案 1 :(得分:0)

试试此代码

string query = "select * from test.user where replace(email,' ','')='"
+ emailtextbox.Text.Trim() + "'";

MySqlConnection conDatabase = new MySqlConnection(constring);
MySqlCommand cmddatabase = new MySqlCommand(query, conDatabase);