简体   繁体   中英

Unhandled Exception When Executing SQL Server INSERT

I have written code in C# and integrated it with SQL Server, but each time I run it I keep getting this error:

System.Data.SqlClient.SqlException: 'Incorrect syntax near ';'.'

My code is this:

private void btnsubmit_Click(object sender, EventArgs e)
{
    var connectiontring =
        ConfigurationManager.ConnectionStrings["connData"].ConnectionString;

    string querystring = "INSERT INTO [Table] VALUES(@Name, @Gender, @Age, @Course, @Department, @Program, @Address, @PostCode, @Email;";

    using (var connection = new SqlConnection(connectiontring))
    {
        var cmd = new SqlCommand(querystring, connection);
        connection.Open();

        cmd.Parameters.AddWithValue("Name", tblName.Text);
        ...
        cmd.Parameters.AddWithValue("Email", tbllMail.Text);

        cmd.ExecuteReader();

        tblName.Text = "";
        ...
        tbllMail.Text = "";
    }
}

SQL Server is right. Add a closing paren after the list of values:

string querystring = "insert into [Table] VALUES(@Name, ..., @Email);";

You have a ; in the query string in place where ) should appear. Correct query string is:

 string querystring = 
    "insert into [Table] VALUES " +
    "   (@Name, @Gender, @Age, @Course, @Department, @Program, " +
    "    @Address, @PostCode, @Email)";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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