我在 .net 中有一个类来使用 Odbc 连接 Access,但我想升级到 OleDb
odbc 代码是:
Imports System.Data.Odbc
Imports System
Public Class DatabaseFunctions
Shared con As OdbcConnection
Const conString = "Dsn=name;uid=sa;pwd=password;"
Public Shared Function GetDataReader(ByVal SQL As String) As OdbcDataReader
Dim dr As OdbcDataReader
Try
con = New OdbcConnection
con.ConnectionString = conString
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("Error:" & ex.Message, "TITLE", 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 GetDataReaderVal(ByVal SQL As String) As String
Dim result As String
Try
con = New OdbcConnection
con.ConnectionString = conString
con.Open()
Dim cmd As New OdbcCommand(SQL, con)
Dim dr As OdbcDataReader
result = ""
dr = cmd.ExecuteReader
dr.Read()
resultado = dr.GetValue(0).ToString
cmd.Connection.Close()
cmd.Connection.Dispose()
cmd.Connection = Nothing
cmd.Parameters.Clear()
cmd.Dispose()
cmd = Nothing
con.Close()
con.Dispose()
Return resultado
Catch ex As Exception
MessageBox.Show("Error GETTING DATA:" & ex.Message, "TITLE", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
If (con.State = System.Data.ConnectionState.Open) Then
con.Close()
con.Dispose()
End If
End Try
resultado = "0"
Return resultado
End Function
Public Shared Function GetDataTable(ByVal SQL As String) As DataTable
Dim table As New DataTable
Try
con = New OdbcConnection
con.ConnectionString = conString
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("Error GETTING TABLE:" & ex.Message, "TITLE", 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 = conString
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("Error EXE QUERY:" & ex.Message, "TITLE", 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
我在做:
Imports System.Data.OleDb
Public Class DatabaseFunctions
Dim Provider As String
Public OleCn As New OleDbConnection()
Public OleDa As New OleDbDataAdapter()
Public Shared Function OledbConnectionString() As String
Provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & My.Application.Info.DirectoryPath & "/dbFile.Accdb;Jet OLEDB:Database Password=password;"
Return Provider
End Function
Public Shared Sub start()
If OleCn.State <> ConnectionState.Open Then
OleCn.ConnectionString = OledbConnectionString()
OleCn.Open()
End If
End Sub
Public Sub finish()
OleCn.Close()
End Sub
Public Shared Sub ExecuteQuery(ByVal SQL As String)
Dim OleDr As OleDbDataReader
OleDa.SelectCommand = New OleDbCommand()
OleDa.SelectCommand.CommandText = SQL
OleDa.SelectCommand.Connection = OleCn
OleDr = OleDa.SelectCommand.ExecuteReader()
OleDr.Close()
End Sub
End Class
我怎样才能像在 odbc 中一样返回一个表、一个值和读取器,我很困惑......