我的项目中有一个SQL插入查询,它工作正常,实际上可能有点太好了,因为它保存了我输入的数据,但保存了两次,每个都有两个不同的ID自动编号。任何人都可以找出原因吗?
//保存按钮代码
Private Sub buttonSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim incidentSolved As Boolean = False
If cboxSolved.Checked Then
incidentSolved = True
End If
If txtClientSave.Text = "" Then
MsgBox("Client name cannot be blank")
ElseIf rtbProblem.Text = "" Then
MsgBox("Problem cannot be blank")
ElseIf cboxSolved.Checked = True And rtbSolution.Text = "" Then
MsgBox("Please enter solution")
Else
database.SaveNewIncident(txtClientSave.Text, dtpStart.Value, dtpEnd.Value, rtbProblem.Text, dtpStartTime.Value, dtpEndTime.Value, cboxSolved.Checked, rtbSolution.Text, _con)
MsgBox("Save successful")
txtClientSave.Text = ""
rtbProblem.Text = ""
rtbSolution.Text = ""
dtpStart.Value = Date.Today
dtpEnd.Value = Date.Today
dtpStartTime.Value = DateTime.Now
dtpEndTime.Value = DateTime.Now
cboxSolved.Checked = False
End If
loadIncidents()
End Sub
//保存SQL
Public Shared Function SaveNewIncident(ByVal clientName As String, dateStart As Date, dateEnd As Date, ByVal incidentProblem As String, ByVal timeStart As String, ByVal timeEnd As String, ByVal incidentSolved As Boolean, ByVal incidentSolution As String, _Con As OleDbConnection)
Dim tr As OleDbTransaction = Nothing
Try
tr = _Con.BeginTransaction()
Dim Dc As New OleDbCommand
Dc.Connection = _Con
Dc.CommandType = CommandType.Text
Dc.CommandText = "INSERT INTO dbo.tblIncidents VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)"
Dc.Transaction = tr
Dc.Parameters.Add("@clientName", OleDbType.VarChar).Value = clientName
Dc.Parameters.Add("@dateStart", OleDbType.Date).Value = dateStart
Dc.Parameters.Add("@dateEnd", OleDbType.Date).Value = dateEnd
Dc.Parameters.Add("@incidentProblem", OleDbType.LongVarChar).Value = incidentProblem
Dc.Parameters.Add("@timeStart", OleDbType.VarChar).Value = timeStart
Dc.Parameters.Add("@timeEnd", OleDbType.VarChar).Value = timeEnd
Dc.Parameters.Add("@incidentSolved", OleDbType.Boolean).Value = incidentSolved
Dc.Parameters.Add("@incidentSolution", OleDbType.LongVarChar).Value = incidentSolution
Dc.Parameters.Add("@SSMA_TimeStamp", OleDbType.DBTimeStamp)
Dim supportID As Integer
Dim Ds As New OleDbCommand
Ds.CommandType = CommandType.Text
Ds.CommandText = "SELECT SCOPE_IDENTITY() AS supportID"
Ds.Transaction = tr
supportID = CType(Dc.ExecuteScalar(), Integer)
Dc.ExecuteNonQuery()
tr.Commit()
Catch ex As Exception
mdInit.errorLog(ex.Message, ex.StackTrace)
MsgBox("Failed to save data, refer to error log")
tr.Rollback()
End Try
End Function
答案 0 :(得分:3)
supportID = CType(Dc.ExecuteScalar(), Integer)
Dc.ExecuteNonQuery() 'you are doing it a second time here
就是这样,你正在执行它两次
评论你的第二个
supportID = CType(Dc.ExecuteScalar(), Integer)
'Dc.ExecuteNonQuery()
问题出在SaveNewIncident中,你执行标量并获得生成的supportId,然后你再次执行整个事件作为非查询 - 两者都是执行命令的有效方法,但INSERT和SELECT命令都执行两次(在第二个执行中,ExecuteNonQuery,你没有选择所选的值)
怎么样
Public Shared Function SaveNewIncident(ByVal clientName As String, dateStart As Date, dateEnd As Date, ByVal incidentProblem As String, ByVal timeStart As String, ByVal timeEnd As String,
ByVal incidentSolved As Boolean, ByVal incidentSolution As String, _Con As OleDbConnection)
Dim tr As OleDbTransaction = Nothing
Try
tr = _Con.BeginTransaction()
Dim Dc As New OleDbCommand
Dc.Connection = _Con
Dc.CommandType = CommandType.Text
Dc.CommandText = "INSERT INTO dbo.tblIncidents VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)"
Dc.Transaction = tr
Dc.Parameters.Add("@clientName", OleDbType.VarChar).Value = clientName
Dc.Parameters.Add("@dateStart", OleDbType.Date).Value = dateStart
Dc.Parameters.Add("@dateEnd", OleDbType.Date).Value = dateEnd
Dc.Parameters.Add("@incidentProblem", OleDbType.LongVarChar).Value = incidentProblem
Dc.Parameters.Add("@timeStart", OleDbType.VarChar).Value = timeStart
Dc.Parameters.Add("@timeEnd", OleDbType.VarChar).Value = timeEnd
Dc.Parameters.Add("@incidentSolved", OleDbType.Boolean).Value = incidentSolved
Dc.Parameters.Add("@incidentSolution", OleDbType.LongVarChar).Value = incidentSolution
Dc.Parameters.Add("@SSMA_TimeStamp", OleDbType.DBTimeStamp)
Dim supportID As Integer
Dc.CommandText += ";SELECT SCOPE_IDENTITY() AS supportID"
supportID = CType(Dc.ExecuteScalar(), Integer)
tr.Commit()
Catch ex As Exception
mdInit.errorLog(ex.Message, ex.StackTrace)
MsgBox("Failed to save data, refer to error log")
tr.Rollback()
End Try
结束功能
ds和dc之间也存在混淆
答案 1 :(得分:0)
只需评论Dc.ExecuteNonQuery()
,因为它已在Dc.ExecuteScalar()