我当前的应用程序正在使用基于实例的数据访问层。我用连接字符串实例化层。然后我调用一个方法来执行某种命令。例如,有一种方法可以填充数据集。基本上,我传递存储过程和任何 SQL 参数并取回数据集。有一个静态类来处理我的数据访问还是基于实例更好?我确实有一个包含对象的域层,但我没有像 ORM 那样映射对象。我将对象传递给工厂,然后实例化数据层以拉回数据集。然后我将数据集映射到对象。我计划更新我的应用程序(是的,迁移到 C#),但我没有时间改变整个事情。我对插入更新和删除做同样的事情。如果我现在所做的一切正常,请告诉我。您认为这种方法有什么问题吗?否则,我该怎么办?我没有写这门课。我在网上找到了它,并认为这是我需要的。
以下是数据类的示例:
Public Sub New(ByVal connectionString As String)
_connectionString = connectionString
End Sub
Public Function FillDataset(ByVal cmd As String, ByVal cmdType As CommandType, Optional ByVal parameters() As SqlParameter = Nothing) As DataSet
Dim connection As SqlConnection = Nothing
Dim command As SqlCommand = Nothing
Dim sqlda As SqlDataAdapter = Nothing
Dim res As New DataSet
Try
connection = New SqlConnection(_connectionString)
command = New SqlCommand(cmd, connection)
command.CommandType = cmdType
AssignParameters(command, parameters)
sqlda = New SqlDataAdapter(command)
sqlda.Fill(res)
Catch ex As Exception
'CreateDataEntry(ex, WriteType.ToFile, cmd)
Finally
If Not (connection Is Nothing) Then connection.Dispose()
If Not (command Is Nothing) Then command.Dispose()
If Not (sqlda Is Nothing) Then sqlda.Dispose()
End Try
Return res
End Function
Public Function ExecuteNonQuery(ByVal spname As String, ByVal ParamArray parameterValues() As Object) As Object
Dim connection As SqlConnection = Nothing
Dim command As SqlCommand = Nothing
Dim res As Object = Nothing
Try
connection = New SqlConnection(_connectionString)
command = New SqlCommand(spname, connection)
command.CommandType = CommandType.StoredProcedure
command.Parameters.AddRange(parameterValues)
connection.Open()
command.ExecuteNonQuery()
res = command.Parameters(command.Parameters.Count - 1).Value
Catch ex As Exception
CreateDataEntry(ex, WriteType.ToFile, spname)
If Not (transaction Is Nothing) Then
transaction.Rollback()
End If
Finally
If Not (connection Is Nothing) AndAlso (connection.State = ConnectionState.Open) Then connection.Close()
If Not (command Is Nothing) Then command.Dispose()
End Try
Return res
End Function