0

我正在使用 oledb 数据适配器来更新 access 数据库,其中显示(并可能编辑)表单上的 datagridview 中的信息。stock update 命令(来自commandbuilder)确实有效并更新了已修改的数据。但是,数据表中有两列分别称为“updatedBy”“updatedDate”我希望始终填写当前用户和当前日期。我不希望用户输入此信息并可能输入虚假值,例如“我”和“现在”。所以我的问题是,如何修改更新命令以在任何更改的行上执行此操作?我尝试制作由命令生成器生成的 sql 的副本,并简单地为这些项目插入我的变量,但这不起作用。下面是我的“确定”按钮的代码,你可以看到我尝试过的一些事情。抱歉,我从命令生成器中复制了非常长的 sql 行,我希望它清晰且有意义。感谢您的任何指示。

Private Sub OkButton_Click(sender As Object, e As EventArgs) Handles OkButton.Click

        Dim count As Integer = 0
        Dim sqlcommand As String = String.Empty

        'if there are "dirty" rows, manually update the "updated" fields in the DGV before calling update command

        If _DGVchanged Then
            Using con As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & VE_docPath & VE_currentProject & ".accdb;Persist Security Info=True")
                Using cmd As New OleDb.OleDbCommand("", con)
                    Try
                        cmd.CommandText = "(SELECT * FROM PartDefinitions)"
                        cmd.CommandType = CommandType.Text
                        cmd.Connection = con

                        con.Open()
                        _partsDataAdapter.SelectCommand = cmd
                        'command text extracted update command, works ok
                        sqlcommand = "UPDATE PartDefinitions SET PartClass = ?, PartNumber = ?, PartNumberAlt = ?, Description = ?, ShellType = ?, ElementCount = ?, TerminalCount = ?, KitNumber = ?, StockNumber = ?, WiringPartName = ?, ArticleName = ?, Manufacturer = ?, ManufacturerID = ?, Supplier = ?, FamilyCode = ?, UseVoltage = ?, CoVoltage = ?, UseFrequency = ?, CoFrequency = ?, IsObsolete = ?, Series = ?, SymbolName = ?, PartType = ?, Weight = ?, WeightUnits = ?, CostEach = ?, ConnectionType = ?, MaxSection = ?, MaxGuage = ?, MinSection = ?, MinGauge = ?, InsertedBy = ?, InsertedDate = ?, UpdatedBy = ?, UpdatedDate = ? WHERE ((PartDefID = ?) And ((? = 1 And PartClass Is NULL) Or (PartClass = ?)) And ((? = 1 And ElementCount Is NULL) Or (ElementCount = ?)) And ((? = 1 And TerminalCount Is NULL) Or (TerminalCount = ?)) And ((? = 1 And ManufacturerID Is NULL) Or (ManufacturerID = ?)) And ((? = 1 And IsObsolete Is NULL) Or (IsObsolete = ?)) And ((? = 1 And PartType Is NULL) Or (PartType = ?)) And ((? = 1 And Weight Is NULL) Or (Weight = ?)) And ((? = 1 And CostEach Is NULL) Or (CostEach = ?)) And ((? = 1 And MaxSection Is NULL) Or (MaxSection = ?)) And ((? = 1 And MaxGuage Is NULL) Or (MaxGuage = ?)) And ((? = 1 And MinSection Is NULL) Or (MinSection = ?)) And ((? = 1 And MinGauge Is NULL) Or (MinGauge = ?)) And ((? = 1 And InsertedDate Is NULL) Or (InsertedDate = ?)) And ((? = 1 And UpdatedDate Is NULL) Or (UpdatedDate = ?)))"
                        'tried to insert my own value, does NOT work
                        sqlcommand = "UPDATE PartDefinitions SET PartClass = ?, PartNumber = ?, PartNumberAlt = ?, Description = ?, ShellType = ?, ElementCount = ?, TerminalCount = ?, KitNumber = ?, StockNumber = ?, WiringPartName = ?, ArticleName = ?, Manufacturer = ?, ManufacturerID = ?, Supplier = ?, FamilyCode = ?, UseVoltage = ?, CoVoltage = ?, UseFrequency = ?, CoFrequency = ?, IsObsolete = ?, Series = ?, SymbolName = ?, PartType = ?, Weight = ?, WeightUnits = ?, CostEach = ?, ConnectionType = ?, MaxSection = ?, MaxGuage = ?, MinSection = ?, MinGauge = ?, InsertedBy = ?, InsertedDate = ?, UpdatedBy = '" & VE_currentUser & "', UpdatedDate = '" & DateTime.Now & "' WHERE ((PartDefID = ?) And ((? = 1 And PartClass Is NULL) Or (PartClass = ?)) And ((? = 1 And ElementCount Is NULL) Or (ElementCount = ?)) And ((? = 1 And TerminalCount Is NULL) Or (TerminalCount = ?)) And ((? = 1 And ManufacturerID Is NULL) Or (ManufacturerID = ?)) And ((? = 1 And IsObsolete Is NULL) Or (IsObsolete = ?)) And ((? = 1 And PartType Is NULL) Or (PartType = ?)) And ((? = 1 And Weight Is NULL) Or (Weight = ?)) And ((? = 1 And CostEach Is NULL) Or (CostEach = ?)) And ((? = 1 And MaxSection Is NULL) Or (MaxSection = ?)) And ((? = 1 And MaxGuage Is NULL) Or (MaxGuage = ?)) And ((? = 1 And MinSection Is NULL) Or (MinSection = ?)) And ((? = 1 And MinGauge Is NULL) Or (MinGauge = ?)) And ((? = 1 And InsertedDate Is NULL) Or (InsertedDate = ?)) And ((? = 1 And UpdatedDate Is NULL) Or (UpdatedDate = ?)))"
                        'this simplified version didnt work either
                        sqlcommand = "UPDATE PartDefinitions Set PartClass = ?, PartNumber = ?, PartNumberAlt = ?, Description = ?, ShellType = ?, ElementCount = ?, TerminalCount = ?, KitNumber = ?, StockNumber = ?, WiringPartName = ?, ArticleName = ?, Manufacturer = ?, ManufacturerID = ?, Supplier = ?, FamilyCode = ?, UseVoltage = ?, CoVoltage = ?, UseFrequency = ?, CoFrequency = ?, IsObsolete = ?, Series = ?, SymbolName = ?, PartType = ?, Weight = ?, WeightUnits = ?, CostEach = ?, ConnectionType = ?, MaxSection = ?, MaxGuage = ?, MinSection = ?, MinGauge = ?, InsertedBy = ?, InsertedDate = ?, UpdatedBy = '" & VE_currentUser & "', **UpdatedDate = '" & DateTime.Now & "'**  WHERE ((PartDefID = ?) And (PartClass = ?) And (ElementCount = ?) And (TerminalCount = ?) And (ManufacturerID = ?) And (IsObsolete = ?)) And (PartType = ?) And (Weight = ?) And (CostEach = ?) And (MaxSection = ?) And (MaxGuage = ?) And (MinSection = ?) And (MinGauge = ?) And (InsertedDate = NULL) And (UpdatedDate = NULL))"

                        '_partsDataAdapter.UpdateCommand = New OleDb.OleDbCommand(sqlcommand, con)
                        _partsDataAdapter.UpdateCommand = New OleDbCommandBuilder(_partsDataAdapter).GetUpdateCommand()

                        'debugging tool to see what the command text is
                        Dim test As String = _partsDataAdapter.UpdateCommand.CommandText


                        'I can manually edit rows in the data table and it will work, but first have to determine which rows have been edited in the DGV.
                        '_PartsDataTable.Rows(0).Item("UpdatedBy") = "joe kidd"


                        Me.Validate()
                        Me.BindingSource1.EndEdit()

                        count = _partsDataAdapter.Update(_PartsDataTable) 'get number of affected rows
                        MessageBox.Show("Updated " & count & "rows")

                    Catch SqlError As System.Data.SqlTypes.SqlTypeException
                        MsgBox(SqlError.Message, MsgBoxStyle.OkOnly, "Error")

                    Catch ex As Exception
                        MsgBox(ex.Message, MsgBoxStyle.OkOnly, "Error")
                    End Try
                End Using
            End Using
        End If

        Me.Close()
        Me.Dispose()
    End Sub
4

0 回答 0