2017-04-04 68 views
3

我是使用Asp.net MVC5的初學者,我試圖將記錄更新到數據庫,並且始終顯示。在System.Data.dll中發生類型'System.Data.SqlClient.SqlException'的異常

下面是截圖

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(); 

強大的文本

+2

「@ MiddleName」後的逗號# – ninja

回答

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(); 
+0

謝謝!有用:) –

相關問題