【发布时间】:2016-07-26 10:50:10
【问题描述】:
我的项目中有一个 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
【问题讨论】:
标签: sql-server vb.net sql-server-2008 insert