0

我有一个类来处理 vb.net 中的数据库

所以我认为这是正确的,但是程序有时没有正确插入表单中的数据以访问表这个事件非常罕见所以我有插入36个注册表然后没有插入37个的经验,然后我重新启动程序,我可以再次插入......

这可能是什么原因?

我目前有这个命令来处理执行

    con = New OdbcConnection
    con.ConnectionString = "Dsn=A;uid=sa;pwd=A;"
    con.Open()
    Dim cmd As New OdbcCommand(SQL, con)
    dr = cmd.ExecuteReader
    cmd.Connection.Close()
    cmd.Connection.Dispose()
    cmd.Connection = Nothing
    cmd.Parameters.Clear()
    cmd.Dispose()
    cmd = Nothing
    con.Close()
    con.Dispose()

我有

Public Shared Function GetDataReader(ByVal SQL As String) As OdbcDataReader
        Dim dr As OdbcDataReader
        Try
            con = New OdbcConnection
            con.ConnectionString = "Dsn=A;uid=sa;pwd=A;"
            con.Open()
            Dim cmd As New OdbcCommand(SQL, con)
            dr = cmd.ExecuteReader
            cmd.Connection.Close()
            cmd.Connection.Dispose()
            cmd.Connection = Nothing
            cmd.Parameters.Clear()
            cmd.Dispose()
            cmd = Nothing
            con.Close()
            con.Dispose()
            Return dr
        Catch ex As Exception
            MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            If (con.State = System.Data.ConnectionState.Open) Then
                con.Close()
                con.Dispose()
            End If
        End Try
        dr = Nothing
        Return dr
    End Function


Public Shared Function GetDataTable(ByVal SQL As String) As DataTable
        Dim table As New DataTable

        Try
            con = New OdbcConnection
            con.ConnectionString = "Dsn=A;uid=sa;pwd=A;"
            con.Open()
            Dim cmd As New OdbcCommand(SQL, con)

            Dim da As New OdbcDataAdapter(cmd)
            da.Fill(table)
            cmd.Connection.Close()
            cmd.Connection.Dispose()
            cmd.Connection = Nothing
            cmd.Parameters.Clear()
            cmd.Dispose()
            cmd = Nothing
            con.Close()
            con.Dispose()
            Return table
        Catch ex As Exception
            MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            If (con.State = System.Data.ConnectionState.Open) Then
                con.Close()
                con.Dispose()
            End If
        End Try
        Return table
    End Function



    Public Shared Sub ExecuteQuery(ByVal SQL As String)
        Try
            con = New OdbcConnection
            con.ConnectionString = "Dsn=A;uid=sa;pwd=A;"
            con.Open()
            Dim cmd As New OdbcCommand(SQL, con)
            cmd.ExecuteNonQuery()
            cmd.Connection.Close()
            cmd.Connection.Dispose()
            cmd.Connection = Nothing
            cmd.Parameters.Clear()
            cmd.Dispose()
            cmd = Nothing
            con.Close()
            con.Dispose()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            If (con.State = System.Data.ConnectionState.Open) Then
                con.Close()
                con.Dispose()
            End If
        End Try
    End Sub
End Class
4

0 回答 0