在数据库中保存数据时,出现“错误:System.Data.SqlClient.SqlException:'('。附近的语法不正确。”错误

时间:2019-08-15 10:19:27

标签: string insert-query

由于出现以下错误,我无法将数据保存在数据库中:

获取错误:

  

System.Data.SqlClient.SqlException:'('附近的语法不正确。

     

在System.Data.SqlClient.SqlConnection.OnError(SqlException异常,布尔值breakConnection)
  在System.Data.SqlClient.SqlInternalConnection.OnError(SqlException异常,布尔breakConnection)
  在System.Data.SqlClient.TdsParser.Run(System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)处   在System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,RunBehavior runBehavior,字符串resetOptionsString)
  在System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,Boolean异步)
  在System.Data.SqlClient.SqlCommand.RunExecuteReader处(CommandBehavior cmdBehavior,RunBehavior runBehavior,布尔值returnStream,String方法,DbAsyncResult结果)
  在System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult结果,字符串methodName,布尔sendToPipe)
  在System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
  在Medicine_Search.Registration.Button1_Click(对象发送者,EventArgs e)   在C:\ Users \ Asus \ source \ repos \ Medicine_Search \ Medicine_Search \ Registration.aspx.cs:第54行

我的代码:

protected void Button1_Click(object sender, EventArgs e)
{
    try
    {
            Guid newGUID = Guid.NewGuid();

            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);

            con.Open();

            string insertQuery = "insert into (ID, Username, Password, ConfirmPassword, Email, Area, Address, City, Contactno, SecurityQuestion, Answer) values (@ID, @Username, @Password, @ConfirmPassword, @Email, @Area, @Address, @City, @Contactno, @SecurityQuestion, @Answer)";

            SqlCommand com = new SqlCommand(insertQuery, con);

            com.Parameters.AddWithValue("@ID", newGUID.ToString());
            com.Parameters.AddWithValue("@Username", TextBoxUN.Text);
            com.Parameters.AddWithValue("@Password", TextBoxPass.Text);
            com.Parameters.AddWithValue("@ConfirmPassword", TextBoxCPass.Text);
            com.Parameters.AddWithValue("@Email", TextBoxEmail.Text);
            com.Parameters.AddWithValue("@Area", TextBoxArea.Text);
            com.Parameters.AddWithValue("@Address", TextBoxAdd.Text);
            com.Parameters.AddWithValue("@City", TextBoxCity.Text);
            com.Parameters.AddWithValue("@Contactno", TextBoxCno.Text);
            com.Parameters.AddWithValue("@SecurityQuestion", DropDownListSQ.SelectedItem.ToString());
            com.Parameters.AddWithValue("@Answer", TextBoxAns.Text);

            com.ExecuteNonQuery();   // Here I get the error

            Response.Redirect("Admin.aspx");
            Response.Write("Your Registration was Successful");
            con.Close();
        }
        catch (Exception ex)
        {
            Response.Write("Error:" + ex.ToString());
        }
    }
}

如何将数据保存在数据库中?

0 个答案:

没有答案