Flash fernando
Flash fernando

Reputation: 13

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!");
}

Upvotes: 0

Views: 222

Answers (1)

Ipsit Gaur
Ipsit Gaur

Reputation: 2927

You need to write like this

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

This is the way the SQL insert works.

Upvotes: 3

Related Questions