使用vb.net更新多表访问数据库

时间:2013-09-27 11:38:39

标签: vb.net sql-update multi-table

有人可以帮助我吗? 我的问题是,当我在下一个构造中使用dataAdapter.Update时,我得到的返回值为零(没有更新行,但没有原因或错误返回)。

当我使用Access运行它时,更新sentense有效,当我在单个表上工作时,其他更新请求正常工作。

我正在使用: vb.net \ visual studio 2012 \ access 2010 DB

我在DataGridView上更新数据并调用更新函数:

更新命令:

Private Function createGeneralUpdateStatement() As String 
Return "UPDATE EntitysDataTbl INNER JOIN ManagersExtraDataTbl ON EntitysDataTbl.entityID = ManagersExtraDataTbl.managerID " + _
"SET EntitysDataTbl.entityUserName = [@EntitysDataTbl].entityUserName, " + _
"EntitysDataTbl.entityLastEntry = [@EntitysDataTbl].entityLastEntry, " + _
"ManagersExtraDataTbl.mPassword = [@ManagersExtraDataTbl].mPassword, " + _
"ManagersExtraDataTbl.workGroup = [@ManagersExtraDataTbl].workGroup, " + _
"ManagersExtraDataTbl.entityAccessType = [@ManagersExtraDataTbl].entityAccessType, " + _
"ManagersExtraDataTbl.mActive = [@ManagersExtraDataTbl].mActive" + _
"WHERE EntitysDataTbl.entityID= [@EntitysDataTbl].entityID"
End Function

参数是:

Dim parmList(2) As String
        parmList(0) = "mPassword"
        parmList(1) = "entityUserName"

,更新功能是:

   Public Function updateDB(ByVal updateCommand As String, ByVal parmList() As String, Optional ByVal insertCommand As String = "") As Boolean
        Dim res As Boolean = False

        SyncLock Me
            Try
                mainDataSet.EndInit()
                mUpdateCommand = New OleDb.OleDbCommand(updateCommand, MyBase.getConnector)

                For Each Str As String In parmList
                    If Not Str Is Nothing Then
                        If (Str.StartsWith("%")) Then
                            mUpdateCommand.Parameters.Add("@" & Str.Trim("%"), OleDb.OleDbType.Boolean, MAX_COL_LEN, Str.Trim("%"))
                        Else
                            mUpdateCommand.Parameters.Add("[@" & Str & "]", OleDb.OleDbType.VarChar, MAX_COL_LEN, Str)
                        End If

                    End If
                Next
                mDataAdapter.UpdateCommand = mUpdateCommand

                If (Not insertCommand.Equals("")) Then

                    mInsertCommand = New OleDb.OleDbCommand(insertCommand, MyBase.getConnector)
                    For Each Str As String In parmList
                        If Not Str Is Nothing Then
                            'If Str.Equals("RuleIDNum") Then
                            '    Continue For
                            'End If
                            If (Str.StartsWith("%")) Then
                                mInsertCommand.Parameters.Add("@" & Str.Trim("%"), OleDb.OleDbType.Boolean, MAX_COL_LEN, Str.Trim("%"))
                            Else
                                mInsertCommand.Parameters.Add("[@" & Str & "]", OleDb.OleDbType.VarChar, MAX_COL_LEN, Str)
                            End If
                        End If
                    Next
                    mDataAdapter.InsertCommand = mInsertCommand
                End If

                    Dim i As Integer = mDataAdapter.Update(mainDataSet)
    'ERROR - i = 0 => NO LINE WAS UPDATED!!!!
                    res = True
                Catch ex As Exception
                res = False
            End Try

            mDBWasUpdated = res
        End SyncLock

        Return res

    End Function

0 个答案:

没有答案