我正在使用sql server 2005和visual stdio 2008 我的页面中有一个文本框作为txtEmailId 我想将数据库中的这个值与email_id列进行比较[它是主键] 使用自定义验证器
,在单击按钮时避免数据库不一致答案 0 :(得分:3)
有几种方法。
1:使用sqlcommand执行db查询,如下所示:
SqlDataReader reader = null;
SqlConnection conn = new SqlConnection("Yourconnectionstring");
conn.Open();
SqlCommand cmd = new SqlCommand("select * from yourtable where email_id=@emailid", conn);
cmd.Parameters.AddWithValue("@emailid",txtEmail.Text);
reader = cmd.ExecuteReader();
if(reader!=null && reader.HasRows){
//email exists in db do something
}
答案 1 :(得分:1)
我的语法可能已关闭,但这是你要找的吗?
如果txtEmailID.Text == email_id
performActionA;
否则
performActionB;
答案 2 :(得分:0)
SOLUTION :>
ValidateQuery = "Select [Email_Id] from Sign_Up where (Email_Id = '"+txtEmailId.Text+"')";
SqlCommand Validatecmd = new SqlCommand(ValidateQuery, con);
String validate_email;
validate_email= (String)Validatecmd.ExecuteScalar();
if (validate_email != null)
{
lblValidateEmail.Text = "YOUR EMAIL ID IS REGISTERD TRY DIFFERENT EMAIL ID ";
}
else
{
// DO WHAT EVER U WANT
}</code>