更新按钮在VB中不起作用。而且没有错误显示

时间:2019-11-01 16:17:01

标签: vb.net

def f(i: Int) = i + 1

// if x = Left, then z = 0
// if x = Right, then z = x + 1
val z = x.fold(_ => 0, i => i + 1)

1 个答案:

答案 0 :(得分:6)

该代码中有一些非常糟糕模式,因此经常更新代码以使用更好的做法将有助于发现潜在的问题。您需要阅读以下评论,以了解具体操作方法:

'Isolate and group the DB access in it's own class, module, or class library.
'ALL access to the database in the application should run through here.
Public Module DB

    'Move these out of the individual methods, so you don't 
    'have to repeat them everywhere
    Private Readonly Property provider As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
    Private Readonly Property datafile As String = "C:\Users\Asus\Documents\Visual Studio 2008\Projects\WindowsApplication7\WindowsApplication7\Tea.accdb"
    Private Readonly Property connString As String = provider & datafile

    'Each query gets its own public method in this module, 
    'but everything else should be marked Private
    Public Sub UpdateEmployeeStatus(NewStatus As String, EmployeeID As Integer)
        'Ole provider for ADO.Net uses ? As parameter placeholders.
        'This string could be a CONSTANT!
        Dim sql As String = "UPDATE [HR_Employee_Details] SET [Status]= ? WHERE [EmployeeID]= ?"

        'The Using block will guarantee your connection is 
        'closed, even if an exception is thrown.
        'The old code could have leaked connection objects 
        'when there were exceptions.
        'Also, ADO.Net works **much** better when you 
        'create new connection objects for most queries.
        Using conn As New OleDbConnection(connString), _
              cmd As New OleDbCommand(sql, conn)

            'I have to guess at parameter types here.
            'You should change this to use exact types & 
            'lengths from the database.
            'ADO.Net will figure out what value goes to 
            'what parameter based on the order of the 
            'placeholders in the sql string.
            'These parameters are the ONLY correct 
            'way to include data in an sql command. 
            'Anything else leaves you open to sql injection.
            cmd.Parameters.Add("?", OleDbType.VarWChar, 20).Value = NewStatus
            'If this is not an integer, change it to correct type.
            'But I wanted to be sure to show two different 
            'parameter types
            cmd.Parameters.Add("?", OleDbType.Integer).Value = EmployeeID

            'wait as long as possible to call Open
            conn.Open()
            cmd.ExecuteNonQuery()
        End Using 'No need to call conn.Close() -- the Using block took care of it
    End Sub
End Module

Private Sub btnHREmployeeInfoUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHREmployeeInfoUpdate.Click
    'Exception handling can still live in the UI
    Try
        'Again: integer here is just to demonstrate a 
        'different parameter type.
        'If your employee IDs really are strings, it's 
        'easy to remove this.
        Dim EmployeeID As Integer = Integer.Parse(txtHREmployeeInfoNewEmployeeID.Text)
        DB.UpdateEmployeeStatus(cboHREmployeeInfoStatus.Text, EmployeeID)
        MsgBox("Updated!", MsgBoxStyle.OkOnly, "Updated")
    Catch ex As Exception
        'This is where your real problem was!
        'This catch block hid the detail from the exception 
        'you would need to solve the problem!
        'If you don't want to show this to the user, which 
        'is reasonable, you can at least log it somewhere.
        'Once you see the exception message, it will probably 
        'be much clearer how to fix the issue.
        MsgBox($"Error updating the database:{vbCrLf}{Ex.Message}", MsgBoxStyle.OkOnly, "Not Updated")
    End Try    
End Sub

但是您不会觉得“好的”代码需要那么长,这里又是删除了注释的地方。特别要注意事件处理程序方法变得多么干净。

Public Module DB
    Private Readonly Property provider As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
    Private Readonly Property datafile As String = "C:\Users\Asus\Documents\Visual Studio 2008\Projects\WindowsApplication7\WindowsApplication7\Tea.accdb"
    Private Readonly Property connString As String = provider & datafile

    Public Sub UpdateEmployeeStatus(NewStatus As String, EmployeeID As Integer)
        Dim sql As String = "UPDATE [HR_Employee_Details] SET [Status]= ? WHERE [EmployeeID]= ?"

        Using conn As New OleDbConnection(connString), _
              cmd As New OleDbCommand(sql, conn)

            cmd.Parameters.Add("?", OleDbType.VarWChar, 20).Value = NewStatus
            cmd.Parameters.Add("?", OleDbType.Integer).Value = EmployeeID

            conn.Open()
            cmd.ExecuteNonQuery()
        End Using
    End Sub
End Module

Private Sub btnHREmployeeInfoUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHREmployeeInfoUpdate.Click
    Try
        Dim EmployeeID As Integer = Integer.Parse(txtHREmployeeInfoNewEmployeeID.Text)
        DB.UpdateEmployeeStatus(cboHREmployeeInfoStatus.Text, EmployeeID)
        MsgBox("Updated!", MsgBoxStyle.OkOnly, "Updated")
    Catch ex As Exception
        MsgBox($"Error updating the database:{vbCrLf}{Ex.Message}", MsgBoxStyle.OkOnly, "Not Updated")
    End Try    
End Sub