System.Data.dll中出现“System.Data.SqlClient.SqlException”类型的异常

时间:2017-04-04 03:37:02

标签: c# asp.net sql-server

我是使用Asp.net MVC5的初学者,我正在尝试将记录更新到数据库,这总是显示出来。

这是截图

enter image description here

以下是代码:

        SqlConnection con = new SqlConnection(DAL.cs);
        con.Open();
        SqlCommand com = new SqlCommand("UPDATE Student SET LastName = @LastName," +
            "FirstName = @FirstName," +
            "MiddleName = @MiddleName," +
            "WHERE ID = @ID", con);

        com.Parameters.AddWithValue("@ID", SqlDbType.Int).Value = s.ID;
        com.Parameters.AddWithValue("@LastName", SqlDbType.VarChar).Value = s.LastName;
        com.Parameters.AddWithValue("@FirstName", SqlDbType.VarChar).Value = s.FirstName;
        com.Parameters.AddWithValue("@MiddleName", SqlDbType.VarChar).Value = s.MiddleName;
        com.ExecuteNonQuery();
        con.Close();

强文

1 个答案:

答案 0 :(得分:1)

删除最后一个参数后的逗号:

SqlConnection con = new SqlConnection(DAL.cs);
con.Open();
SqlCommand com = new SqlCommand("UPDATE Student SET LastName = @LastName," +
        "FirstName = @FirstName," +
        "MiddleName = @MiddleName " +
        "WHERE ID = @ID", con);

com.Parameters.AddWithValue("@ID", SqlDbType.Int).Value = s.ID;
com.Parameters.AddWithValue("@LastName", SqlDbType.VarChar).Value = s.LastName;
com.Parameters.AddWithValue("@FirstName", SqlDbType.VarChar).Value = s.FirstName;
com.Parameters.AddWithValue("@MiddleName", SqlDbType.VarChar).Value = s.MiddleName;
com.ExecuteNonQuery();
con.Close();