根据OracleDataAdapter Class:
线程安全
所有公共静态方法都是线程安全的,尽管实例方法
不保证线程安全。
对于连接,建议仅在需要时才打开连接。如果一个连接持续被使用,保持它打开可能是有意义的。否则,每次都打开/关闭连接。当然,如果你还有其他需要使用连接执行的操作,你可以将连接作为参数传递给方法。但是,请在完成操作后关闭连接。
以下假设已安装 NuGet 包Oracle.ManagedDataAccess。
尝试以下方法之一:
Imports Oracle.ManagedDataAccess.Client
Public Module Helper
Public Function FetchDataDS(connectionStr As String, str_SQL As String) As DataSet
Dim int_Count As Integer = 0
Dim ds As DataSet = New DataSet()
Using connection As OracleConnection = New OracleConnection(connectionStr)
'open
connection.Open()
Using oracleDataAdapter As OracleDataAdapter = New OracleDataAdapter With {
.SelectCommand = New OracleCommand With {
.AddToStatementCache = False,
.NotificationAutoEnlist = False,
.CommandTimeout = 60,
.Connection = connection,
.CommandText = str_SQL
}}
int_Count = oracleDataAdapter.Fill(ds)
End Using
End Using
Return ds
End Function
Public Function FetchDataDS(connection As OracleConnection, str_SQL As String) As DataSet
Dim int_Count As Integer = 0
Dim ds As DataSet = New DataSet()
If connection.State = ConnectionState.Closed Then
'open
connection.Open()
End If
If Not connection.State = ConnectionState.Open Then
Throw New Exception("Error (FetchDataDS) - Connection state is: " & connection.State.ToString())
End If
Using oracleDataAdapter As OracleDataAdapter = New OracleDataAdapter With {
.SelectCommand = New OracleCommand With {
.AddToStatementCache = False,
.NotificationAutoEnlist = False,
.CommandTimeout = 60,
.Connection = connection,
.CommandText = str_SQL
}}
int_Count = oracleDataAdapter.Fill(ds)
End Using
Return ds
End Function
Public Function FetchDataDS(connectionStr As String, str_SQL As String, ds As DataSet) As Integer
Dim int_Count As Integer = 0
Using connection As OracleConnection = New OracleConnection(connectionStr)
'open
connection.Open()
Using oracleDataAdapter As OracleDataAdapter = New OracleDataAdapter With {
.SelectCommand = New OracleCommand With {
.AddToStatementCache = False,
.NotificationAutoEnlist = False,
.CommandTimeout = 60,
.Connection = connection,
.CommandText = str_SQL
}}
int_Count = oracleDataAdapter.Fill(ds)
End Using
End Using
Return int_Count
End Function
Public Function FetchDataDS(connection As OracleConnection, str_SQL As String, ds As DataSet) As Integer
Dim int_Count As Integer = 0
If connection.State = ConnectionState.Closed Then
'open
connection.Open()
End If
If Not connection.State = ConnectionState.Open Then
Throw New Exception("Error (FetchDataDS) - Connection state is: " & connection.State.ToString())
End If
Using oracleDataAdapter As OracleDataAdapter = New OracleDataAdapter With {
.SelectCommand = New OracleCommand With {
.AddToStatementCache = False,
.NotificationAutoEnlist = False,
.CommandTimeout = 60,
.Connection = connection,
.CommandText = str_SQL
}}
int_Count = oracleDataAdapter.Fill(ds)
End Using
Return int_Count
End Function
Public Function FetchDataDT(connectionStr As String, str_SQL As String) As DataTable
Dim int_Count As Integer = 0
Dim dt As DataTable = New DataTable()
Using connection As OracleConnection = New OracleConnection(connectionStr)
'open
connection.Open()
Using oracleDataAdapter As OracleDataAdapter = New OracleDataAdapter With {
.SelectCommand = New OracleCommand With {
.AddToStatementCache = False,
.NotificationAutoEnlist = False,
.CommandTimeout = 60,
.Connection = connection,
.CommandText = str_SQL
}}
int_Count = oracleDataAdapter.Fill(dt)
End Using
End Using
Return dt
End Function
Public Function FetchDataDT(connection As OracleConnection, str_SQL As String) As DataTable
Dim int_Count As Integer = 0
Dim dt As DataTable = New DataTable()
If connection.State = ConnectionState.Closed Then
'open
connection.Open()
End If
If Not connection.State = ConnectionState.Open Then
Throw New Exception("Error (FetchDataDT) - Connection state is: " & connection.State.ToString())
End If
Using oracleDataAdapter As OracleDataAdapter = New OracleDataAdapter With {
.SelectCommand = New OracleCommand With {
.AddToStatementCache = False,
.NotificationAutoEnlist = False,
.CommandTimeout = 60,
.Connection = connection,
.CommandText = str_SQL
}}
int_Count = oracleDataAdapter.Fill(dt)
End Using
Return dt
End Function
Public Function FetchDataDT(connection As OracleConnection, str_SQL As String, dt As DataTable) As Integer
Dim int_Count As Integer = 0
If connection.State = ConnectionState.Closed Then
'open
connection.Open()
End If
If Not connection.State = ConnectionState.Open Then
Throw New Exception("Error (FetchDataDT) - Connection state is: " & connection.State.ToString())
End If
Using oracleDataAdapter As OracleDataAdapter = New OracleDataAdapter With {
.SelectCommand = New OracleCommand With {
.AddToStatementCache = False,
.NotificationAutoEnlist = False,
.CommandTimeout = 60,
.Connection = connection,
.CommandText = str_SQL
}}
int_Count = oracleDataAdapter.Fill(dt)
End Using
Return int_Count
End Function
End Module
无论你在哪里捕获异常,也要捕获 Oracle.ManagedDataAccess.Client.OracleException -
类似于以下内容:
Try
'ToDo: add desired code
...
Catch ex As OracleException
'ToDo: log exception
Catch ex As Exception
'ToDo: log exception
End Try
资源