我们将更改 Settings.vb 中的连接字符串,因此当我们的应用程序在不同于开发计算机的其他计算机上运行时,我们无需担心它是什么。
我们的代码如下所示:
Partial Friend NotInheritable Class MySettings
Dim strComputerName As String
Dim strConnectionString As String
Private Sub MySettings_SettingsLoaded(ByVal sender As Object, ByVal e As System.Configuration.SettingsLoadedEventArgs) Handles Me.SettingsLoaded
' strComputerName =
' Build a new construction string.
'---------------------------------
strConnectionString = "Data Source=" & strComputerName & "\sqlexpress" & _
";Integrated Security=True;User Instance=True"
' Change to the new connection string.
'-------------------------------------
Me.Item("Kemal_Business_SolutionConnectionString") = (strConnectionString)
End Sub
End Class
您能告诉我如何获取计算机名称,因为我们需要将该信息放入连接字符串的“数据源”部分吗?
更新:这是最终编码的样子。谢谢各位的回应:
Partial Friend NotInheritable Class MySettings
Dim strComputerName As String
Dim strConnectionString As String
Private Sub MySettings_SettingsLoaded(ByVal sender As Object, ByVal e As System.Configuration.SettingsLoadedEventArgs) Handles Me.SettingsLoaded
strComputerName = Environment.MachineName
'strComputerName = My.Computer.Name
' Build a new construction string.
'---------------------------------
strConnectionString = "Data Source=" & strComputerName & "\sqlexpress;" & _
"Initial Catalog=""Kemal Business Solution"";" & _
"Integrated Security=True"
' Change to the new connection string.
'-------------------------------------
Me.Item("Kemal_Business_SolutionConnectionString") = (strConnectionString)
End Sub
End Class