我在VB.NET应用程序中使用以下类,我想在每次调用时传递一个新的连接字符串。
Public Class Dataconnect
Private objcon As New SqlConnection("NEW CONNECTION STRING EACH CALL")
Public Shared Function ExecutenonQuery(sqlcmd As SqlCommand) As Integer
Try
Dim objdc As New Dataconnect()
Dim affectedrecord As Integer = 0
If objdc.Opencon() = True Then
sqlcmd.Connection = objdc.objcon
affectedrecord = sqlcmd.ExecuteNonQuery()
objdc.Closecon()
objdc = Nothing
Return affectedrecord
Else
Return affectedrecord
End If
Catch ex As Exception
' new Exception("Error: In ExecuteNonquery");
Throw ex
End Try
End Function
Public Function Opencon() As Boolean
Try
If objcon.State = ConnectionState.Closed Then
objcon.Open()
End If
objcmd.Connection = objcon
Return True
Catch ex As Exception
Throw New Exception("Error: In Open connesction" + ex.Message)
Return False
End Try
End Function
Public Function Closecon() As Boolean
Try If objcon.State = ConnectionState.Open Then
objcon.Close()
End If
objcmd.Dispose()
Return True
Catch ex As Exception
Throw New Exception("Error: In Close connesction" + ex.Message)
Return False
End Try
End Function
End Class
答案 0 :(得分:0)
使用constructor:
Sub New(ByVal s As String)
objcon As New SqlConnection(s)
End Sub
然后:
Dim objdc As New Dataconnect(connectionString)
在此处详细了解类和构造函数:http://www.windowsdevcenter.com/pub/a/dotnet/2002/11/04/vbooppt2.htm