问题描述
我正在尝试更新Episode表中的EpisodeId:117,它成功执行,但是当我检查该表时,它没有更新。 int集ID = 117;
int seriesNumber = 9;
int episodeNumber = 13;
string episodeType = "abnormal episode";
string title = "Reconsideration";
string notes = "recuring behavIoUr";
//connectionString
string connectionString = "data source=LAPTOP-VLO4EFFQ\\MSsqlSERVER01; database=DoctorWho; integrated Security=True;";
//connection using
using (sqlConnection conn = new sqlConnection(connectionString))
{
conn.open();
Console.WriteLine("Connection sucessfull");
string query = "UPDATE tblEpisode " +
"(SeriesNumber,EpisodeNumber,EpisodeType,Title,Notes)" +
"(SET SeriesNumber=@SeriesNumber,EpisodeNumber=@EpisodeNumber,EpisodeType=@EpisodeType,Title=@Title,Notes=@Notes)" +
"(WHERE EpisodeId=@EpisodeId)";
using (sqlCommand command = new sqlCommand(query,conn))
{
//updating data in the sql table with the initial variables
command.Parameters.Add("@EpisodeId",System.Data.sqlDbType.Int).Value = episodeId;
command.Parameters.Add("@SeriesNumber",System.Data.sqlDbType.Int).Value = seriesNumber;
command.Parameters.Add("@EpisodeNumber",System.Data.sqlDbType.Int).Value = episodeNumber;
command.Parameters.Add("@EpisodeType",System.Data.sqlDbType.NVarChar).Value = episodeType;
command.Parameters.Add("@Title",System.Data.sqlDbType.NVarChar).Value = title;
command.Parameters.Add("@Notes",System.Data.sqlDbType.NVarChar).Value = notes;
}
conn.Close();
Console.WriteLine("connection is closed!!");
}
解决方法
您的SQL更新语句存在一些问题。有关SQL LINK
中的更新语句的参考,请参考以下内容还有一种使用AddWithValue方法添加参数的简便方法。 LINK
接下来,您不执行SQL命令。对于Update语句,请使用ExecuteNonQuery()方法。 LINK
也如@ Nikki9696所述,未声明EpisodeId。确保使用其他变量声明EpisodeId。
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim x As Integer
x = DCount("StudentID","tblFeeVoucherGenerate","StudentID =" & Me.StudentID & " ")
If x > 0 Then
MsgBox "Fee Already Recorded for this month"
Me.Undo
Exit Sub
End If
DoCmd.RunCommand acCmdSaveRecord
MsgBox "Record has been Saved"
DoCmd.RunCommand acCmdRecordsGoToNew
End Sub