简体   繁体   中英

Incorrect syntax near '=' on INSERT INTO SQL Server statement in C#

The following code throws the error mentioned in the title, I can't seem to find out what I'm doing wrong. The table is created using Entity Framework and the columns are of type INT which the sent parameters are.

using (SqlConnection conn = new SqlConnection(db.Database.Connection.ConnectionString))
{
    SqlCommand cmd = new SqlCommand("INSERT INTO StudentCourse VALUES (StudentID=@StudentID, CourseID=@CourseID)", conn);

    cmd.Parameters.AddWithValue("@StudentID", studentID);
    cmd.Parameters.AddWithValue("@CourseID", courseID);

    conn.Open();
    int test = cmd.ExecuteNonQuery();
    conn.Close();

    if (test > 0)
        Console.WriteLine("Table updated!");
}

You need to write like this

INSERT INTO StudentCourse (StudentID, CourseID) VALUES(@StudentID, @CourseID)

This is the way the SQL insert works.

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