VB.NET在使用中的应用
我可以做:VB.NET在使用中的应用,vb.net,data-access-layer,using,sqlconnection,Vb.net,Data Access Layer,Using,Sqlconnection,我可以做: using connection myDt1 = sqlSelect(connection, sql) myDt2 = sqlSelect(connection, sql) end using 使用SQLSelect: Public Shared Function SQLSelect(ByVal provider As SqlConnection, ByVal strSQL As String, ByVal ParamArray params() As SqlParameter)
using connection
myDt1 = sqlSelect(connection, sql)
myDt2 = sqlSelect(connection, sql)
end using
使用SQLSelect:
Public Shared Function SQLSelect(ByVal provider As SqlConnection, ByVal strSQL As String, ByVal ParamArray params() As SqlParameter) As DataTable
Using connection = provider
SQLSelect = New DataTable
Dim dtReader As SqlDataReader
Dim command As SqlCommand = CreateSQLCommand(provider, strSQL, params)
Try
connection.Open()
dtReader = command.ExecuteReader()
SQLSelect.Load(dtReader)
dtReader.Close()
Catch ex As Exception
SQLSelect = Nothing
Finally
'If connection.State = ConnectionState.Open Then
' connection.Close()
'End If
command.Dispose()
End Try
End Using '<--- Here drop connection
Return SQLSelect
公共共享函数SQLSelect(ByVal提供程序作为SqlConnection,ByVal strSQL作为String,ByVal paramary params()作为SqlParameter)作为DataTable
使用connection=provider
SQLSelect=新数据表
将dtReader设置为SqlDataReader
Dim命令作为SqlCommand=CreateSQLCommand(提供程序、strSQL、参数)
尝试
connection.Open()
dtReader=command.ExecuteReader()
SQLSelect.Load(dtReader)
dtReader.Close()
特例
SQLSelect=Nothing
最后
'如果connection.State=ConnectionState.Open,则
'连接。关闭()
"完"
command.Dispose()命令
结束尝试
使用“结束只需使用如下DAL模式:
Public Class DataAccessLayer
'Implements IDataAccessLayer
#Region "Members"
Private ReadOnly connectionString As String
Private ReadOnly configuration As IConfiguration
Private ReadOnly exceptionHandler As IExceptionHandler
#End Region
#Region "Constructors"
Public Sub New()
Me.New(New Configuration(), New ExceptionHandler())
End Sub
Public Sub New(configuration As IConfiguration, exceptionHandler As IExceptionHandler)
' You can just pass in a ConnectionString, instead of my IConfiguration
connectionString = configuration.GetDefaultConnectionString()
'Me.exceptionHandler = exceptionHandler
End Sub
#End Region
#Region "Opening and Closing Connection"
Public Function GetConnection() As SqlConnection
Dim conn = New SqlConnection(connectionString)
Try
conn.Open()
Catch ex As SqlException
'exceptionHandler.LogExceptions(ex, Nothing)
Catch ex As Exception
'exceptionHandler.LogExceptions(ex, Nothing)
End Try
Return conn
End Function
#End Region
#Region "Stored Procedure Calls"
Public Function TestConnection() As Boolean
Try
Dim conn = New SqlConnection(connectionString)
conn.Open()
conn.Close()
Catch
Return False
End Try
Return True
End Function
Public Function CallAStoredProcExample() As Boolean
Dim codeObject As CodeObject = Nothing
Try
Using conn = Me.GetConnection()
Using sqlCommand = New SqlCommand("sp_GetCodeByCodeID", conn)
sqlCommand.CommandType = CommandType.StoredProcedure
sqlCommand.Parameters.AddWithValue("@CodeId", 1)
Using dr = sqlCommand.ExecuteReader()
While dr.Read()
codeObject = New CodeObject(dr, True)
End While
End Using
End Using
End Using
Catch ex As Exception
Return False
End Try
Return True
End Function
End Class
呼叫代码:
Dim DAL = New DataAccessLayer(yourConnectionString, Nothing)
DAL.CallAStoredProcExample()
您正在SqlSelect中关闭连接。。使用的全部目的是为您关闭和处理连接。我忘记了注释这两行,在en Using end添加drop connection,这真的很奇怪,因为参数是ByVal,不是吗?如果我每次都传递连接字符串而不是sqlconnection是否更好?您最好使用~connection.Open()~out-SqlSelect并传递SqlSelect一个打开的连接。如果我在不同的类中有3个函数:-Main()->调用例如ListAllData()-ListAllData->executesqlselect(sqlconnection,parameters),那么还要从SqlSelectLast问题中删除Using和EndUsing->哪个打开连接并使用SQLSelect执行最后一个类中的查询?
----SQLSelect->which execute
正确且良好吗?