我在查询中得到sql异常:


  [SqlException(0x80131904):'?'附近的语法不正确。不正确的
  关键字“用户”附近的语法。]


我究竟做错了什么?这个异常意味着什么?

protected void Submit_Click(object sender, EventArgs e)
{
    string myConnection = @"Data Source=REDDEVIL;Initial..."

    SqlConnection conn = new SqlConnection(myConnection);

    HttpPostedFile postedFile = FileUpload1.PostedFile;
    string fileName = Path.GetFileName(postedFile.FileName);
    string fileExtension = Path.GetExtension(fileName);

    if (fileExtension.ToLower() == ".jpg" || fileExtension.ToLower() == ".bmp" ||
        fileExtension.ToLower() == ".gif" || fileExtension.ToLower() == ".png")
    {
        Stream stream = postedFile.InputStream;
        BinaryReader binaryReader = new BinaryReader(stream);
        byte[] bytes = binaryReader.ReadBytes((int)stream.Length);

        string query2 = "INSERT INTO Manager (ID,Name,Address,Phone,Cell,Email,DOB,Commission,Comments,Photo,User ID,IsActive) VALUES (?ID,?Name,?Address,?Phone,?Cell,?Email,?DOB,?Commission,?Comments,?Photo,?User_ID,?IsActive)";
        SqlCommand cmd2 = new SqlCommand(query2, conn);
        cmd2.Parameters.AddWithValue("?ID", mgrID.Text);
        cmd2.Parameters.AddWithValue("?Name", Name.Text);
        cmd2.Parameters.AddWithValue("?Address", address.Text);
        cmd2.Parameters.AddWithValue("?Phone", phoneNo.Text);
        cmd2.Parameters.AddWithValue("?Cell", CellNo.Text);
        cmd2.Parameters.AddWithValue("?Email", email.Text);
        cmd2.Parameters.AddWithValue("?DOB", dob.Text);
        cmd2.Parameters.AddWithValue("?Commission", commission.Text);
        cmd2.Parameters.AddWithValue("?Comments", comments.Text);
        cmd2.Parameters.AddWithValue("?Photo", bytes);
        cmd2.Parameters.AddWithValue("?User_ID", System.DBNull.Value);
        cmd2.Parameters.AddWithValue("?IsActive", System.DBNull.Value);
        conn.Open();
        cmd2.ExecuteNonQuery();
        conn.Close();

        Response.Redirect("~/Views/Portal/Dashboard.aspx");
    }
    else
    {
    }
}


任何帮助都感激不尽。

最佳答案

要指定它是一个参数,您应该在SQL查询中使用@而不是?。然后,在创建参数时,您也不需要?

string query2 = "INSERT INTO Manager (ID,Name,Address,Phone,Cell,Email,DOB,Commission,Comments,Photo,User_ID,IsActive) VALUES (@ID,@Name,@Address,@Phone,@Cell,@Email,@DOB,@Commission,@Comments,@Photo,@User_ID,@IsActive)";

SqlCommand cmd2 = new SqlCommand(query2, conn);
cmd2.Parameters.AddWithValue("ID", mgrID.Text);




正如Tim在本节中所注意到的,您正在指定具有...USER ID,....的字段-我假设您在两者之间缺少_。 (如果确实被称为user,则请参见Tim的[]建议

关于c# - SqlException'?'附近的语法不正确,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39703478/

10-12 20:57