我有一个类来打开一个 sqlconnection 并通过主应用程序引用该连接返回,问题是这个连接总是没有如何修改这个行为:
班级代码:
Public Class Class1
Public ClassConnection As SqlClient.SqlConnection
Public Function OpenConnection() As Object
Try
Dim sConnString As String = ""
sConnString = "Server=ServerName;Database=DBName;User ID=Username;Password=MyPassword"
Using ClassConnection As New SqlClient.SqlConnection
ClassConnection.ConnectionString = sConnString
ClassConnection.Open()
Return ClassConnection
End Using
Catch ex As Exception
Throw ex
Return Nothing
End Try
End Function
结束类
我的表格代码:
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim uu As New Class1
Dim iii As SqlClient.SqlConnection = uu.OpenConnection()
MsgBox(iii.State)
End Sub
结束类