简体   繁体   English

如何检查记录是否已存在于SQL数据库中?

[英]How do I check if the record already exists in SQL database?

I am trying to check if the value exists in a database column. 我正在尝试检查该值是否存在于数据库列中。 I know this question asked several times but none of them is solving my problem. 我知道这个问题曾问过几次,但没有一个能解决我的问题。 I have tried it in many ways. 我已经尝试了很多方法。 Anyway my following code through an exception; 无论如何,我的以下代码都是通过异常产生的;

"An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll Additional information: No mapping exists from object type System.Windows.Forms.TextBox to a known managed provider native type." “在System.Data.dll中发生了类型为'System.ArgumentException'的未处理的异常。附加信息:从对象类型System.Windows.Forms.TextBox到已知的托管提供程序本机类型不存在映射。”

This problem is resolved of exception that was a mistake. 解决了作为错误的异常的问题。 but it still not working although the record is in database but it is not restricting me. 但是,尽管记录在数据库中,但它仍然无法正常工作,但并没有限制我。

Can anyone tell me whats wrong with my code, the code is; 谁能告诉我我的代码有什么问题,代码是;

private void btn_Register_Click(object sender, EventArgs e)
    {
        lbl_Available.Visible = false;
        lbl_Empty.Visible = false;
        bool hasValue1 =  string.IsNullOrWhiteSpace(txt_PinCode.Text);
        bool hasValue2 =  string.IsNullOrWhiteSpace(txt_Name.Text);
        if (!hasValue1 && !hasValue2)
        {
            string pincode = txt_PinCode.Text;
            if (UserExists(pincode))
            {
               lbl_Available.Visible = true;
               return;
            }
            else
            {
                this.Hide();
                RegCluePoint frm = new RegCluePoint();
                frm.lbl_PinCode.Text = txt_PinCode.Text;
                frm.lbl_Name.Text = txt_Name.Text;
                frm.Show();
            }                    
        }
        else
        {
            lbl_Empty.Visible = true;
        }
    }
private bool UserExists(string pincode)
    {
        //pincode = txt_PinCode.Text
        //SqlCommand cmd = new SqlCommand();
        cmd.Connection = conn;
        cmd.CommandText = "SELECT PINCODE from UserInput where PINCODE = '@pincode'";
        cmd.Parameters.AddWithValue("PINCODE", pincode);
        if (conn.State != ConnectionState.Open)
        conn.Open();

        string pinString = (string)cmd.ExecuteScalar();
        conn.Close();

        return (pinString != null);
    } 

您发送txt_PinCode ,一个文本框对象,而不是一个字符串( txt_PinCode.Text在此行中) cmd.Parameters.AddWithValue("PINCODE", txt_PinCode);

You are not passing the pincode that is passed to the method, instead you are passing the textbox itself (not even the value of the textbox). 您没有传递传递给该方法的pincode ,而是传递了文本框本身(甚至不是文本框的值)。

private bool UserExists(string pincode)
{
    //pincode = txt_PinCode.Text
    //SqlCommand cmd = new SqlCommand();
    cmd.Connection = conn;
    cmd.CommandText = "SELECT PINCODE from UserInput where PINCODE = '@pincode'";
    cmd.Parameters.AddWithValue("@pincode", pincode);
    if (conn.State != ConnectionState.Open)
    conn.Open();

    //Execute scalar returns the first column of the first row in the result set
    string pinString = (string)cmd.ExecuteScalar();
    conn.Close();

    return (pinString != null);
} 

You are trying to pass the textbox itself, but not text. 您正在尝试传递文本框本身,而不传递文本。 Change the code 更改密码

cmd.Connection = conn;
cmd.CommandText = "SELECT PINCODE from UserInput where PINCODE = '@pincode'";
cmd.Parameters.AddWithValue("PINCODE", txt_PinCode.Text);

In this line of code you are sending the textbox object onstead of text. 在这行代码中,您将发送文本框对象而不是文本。

cmd.Parameters.AddWithValue("PINCODE", txt_PinCode);

Change it to: 更改为:

cmd.Parameters.AddWithValue("PINCODE", txt_PinCode.Text);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM