我当前的应用程序使用基于实例的数据访问层.我用连接字符串实例化图层.然后我调用一个可以执行某种命令的方法.例如,有一种方法可以填充数据集.基本上,我传递存储过程和任何sql参数并获取数据集.有一个静态类来处理我的数据访问或基于实例的更好吗?我确实有一个带有对象的Domain层,但我没有像ORM那样映射对象.我将对象传递给工厂,然后实例化数据层以撤回数据集.然后我将数据集映射到对象.我计划更新我的应用程序(是的,转向C#),但我没有时间改变整个事情.我对插入更新做了同样的事情,并删除了.如果我现在正在做的事情还可以,请告诉我.你觉得这种方法有什么问题吗?否则,我该怎么办?我没写这堂课.我发现它在线,并认为这是我需要的.
原文链接:https://www.f2er.com/vb/255022.html以下是数据类的示例:
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,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