简体   繁体   English

C#中的INSERT INTO SQL Server语句上'='附近的语法不正确

[英]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. 该表是使用Entity Framework创建的,列的类型为INT ,即所发送的参数。

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. 这就是SQL插入的工作方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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