INSERT查询正在执行但未插入

时间:2016-08-02 08:38:13

标签: sql database vb.net sql-server-2008 insert

我的代码中有以下子例程,用于将新记录插入SQL Server数据库。单步执行后,代码将全部显示,出现消息框,但该行实际上并未插入到数据库中。我知道它正在使用正确的连接,因为我可以查看我使用SQL Server手动插入的相关网格上的记录,并且我的UPDATEDELETE查询也可以使用相同的连接,因此出错了用我的代码,但我无法弄清楚...有人可以帮助我吗?

 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

        Dim ID As Integer
        ID = "1"

        Dc.CommandType = CommandType.Text
        Dc.CommandText = "INSERT INTO dbo.tblIncidents VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)"
        Dc.Transaction = tr
        Dc.Parameters.Add("@supportID", OleDbType.Integer).Value = ID
        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

        tr.Commit()

        MsgBox("Save successful")

    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

1 个答案:

答案 0 :(得分:2)

您尚未执行插入查询。在tr.Commit()

之前添加以下代码
Dc.ExecuteNonQuery()