无法绑定多部分标识符

时间:2011-04-27 21:24:00

标签: asp.net sql

运行此代码时会出现此错误

        SqlConnection con = new SqlConnection(@"Data Source=SAMA-PC\SQLEXPRESS;Initial Catalog=meral10;Integrated Security=True");
        SqlCommand comsel = new SqlCommand("SELECT email from reg where email ="+email_tb.Text,con);
        con.Open();
        comsel.ExecuteNonQuery();
        con.Close();
        if (comsel == null)
        {
            birthday = day_ddl.Text + "/" + month_ddl.Text + "/" + year_ddl.Text;

            SqlCommand com = new SqlCommand("INSERT INTO reg(first_name,last_name,email,email_ver,pass,gender,birthday) values(@fn,@ln,@email,@reemail,@pass,@gen,@birth)", con);
            con.Open();
            com.Parameters.AddWithValue("@fn", firstname_tb.Text);
            com.Parameters.AddWithValue("@ln", lastname_tb.Text);
            com.Parameters.AddWithValue("@email", email_tb.Text);
            com.Parameters.AddWithValue("@reemail", reemail_tb.Text);
            com.Parameters.AddWithValue("@pass", pass_tb.Text);
            com.Parameters.AddWithValue("@gen", gender_ddl.SelectedItem.Text);
            com.Parameters.AddWithValue("@birth", birthday);
            com.ExecuteNonQuery();
            con.Close();}

2 个答案:

答案 0 :(得分:2)

尝试在email_tb.Text附近加上引号,如下所示:

"SELECT email from reg where email ='" + email_tb.Text + "'"

答案 1 :(得分:2)

尝试:

SqlCommand comsel = new SqlCommand("SELECT email from reg where email ='" + email_tb.Text + "'", con)

E.g。你的字符串文字需要在引号中。更好的是,使用SqlParameter!