根据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,.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,.CommandText = str_SQL
}}
int_Count = oracleDataAdapter.Fill(ds)
End Using
End Using
Return int_Count
End Function
Public Function FetchDataDS(connection As OracleConnection,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,.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,.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,.CommandText = str_SQL
}}
int_Count = oracleDataAdapter.Fill(dt)
End Using
Return dt
End Function
Public Function FetchDataDT(connection As OracleConnection,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,.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
资源
本文链接:https://www.f2er.com/9672.html