青春不败g7成员再聚首:vb.net中遇到的问题
来源:百度文库 编辑:高校问答 时间:2024/07/03 09:53:02
class cnt_link
Public cnt As New SqlClient.SqlConnection
Public Function Connect(ByVal strSource As String, ByVal strDBName As String, ByVal strUser As String, ByVal strPasswd As String) As Boolean
Try
cnt.ConnectionString = "Data Source=" & strSource & ";Initial Catalog=" & strDBName & ";User ID=" & strUser & ";Password=" & strPasswd & ";"
cnt.Open()
Catch ex As System.Exception
End Try
End Function
end class
我想在系统运行时,调用这个函数,即初始化一下Connection,以后在每个窗体中都直接用就行,也就是直接 cnt_link.cnt 就可以而不用 new
并且不希望Public shared cnt As New SqlClient.SqlConnection
不知如何实现
不想用到模块(module)
Public cnt As New SqlClient.SqlConnection
Public Function Connect(ByVal strSource As String, ByVal strDBName As String, ByVal strUser As String, ByVal strPasswd As String) As Boolean
Try
cnt.ConnectionString = "Data Source=" & strSource & ";Initial Catalog=" & strDBName & ";User ID=" & strUser & ";Password=" & strPasswd & ";"
cnt.Open()
Catch ex As System.Exception
End Try
End Function
end class
我想在系统运行时,调用这个函数,即初始化一下Connection,以后在每个窗体中都直接用就行,也就是直接 cnt_link.cnt 就可以而不用 new
并且不希望Public shared cnt As New SqlClient.SqlConnection
不知如何实现
不想用到模块(module)
在模块(module)里写一个main函数,把初始化代码放在这里,然后在工程启动里选main()
另外,你的cnt也要定义在模块里作为公共变量