如何将数据库中的值与文本框值进行比较

发布于 2024-10-08 07:53:39 字数 138 浏览 0 评论 0原文

我正在使用 sql server 2005 和 Visual stdio 2008 我的页面中有一个文本框,格式为 txtEmailId 我想将数据库中的这个值与 email_id 列进行比较[它是主键] 为了避免单击按钮时数据库中的不一致,而不使用自定义验证器

I am using sql server 2005 and visual stdio 2008
i have a textbox in my page as txtEmailId
i want to compare this value in database with email_id column[it is a primary key]
to avoid inconsistence in database on a button click with out using custom validator

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

燕归巢 2024-10-15 07:53:39

有几种方法。

1:使用sql命令进行数据库查询,如下所示:

    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
    }

There are several ways.

1: Do a db query using sqlcommand like below:

    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
    }
梅窗月明清似水 2024-10-15 07:53:39

我的语法可能有问题,但这就是您要找的吗?

如果 txtEmailID.Text == email_id
执行动作A;
其他
执行动作B;

My syntax might be off, but is this what you are looking for?

if txtEmailID.Text == email_id
performActionA;
Else
performActionB;

物价感观 2024-10-15 07:53:39
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>
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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文