我們將更改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
你嘗試了'.'或'(本地)'?你也確定每臺電腦都有一個名爲'sqlexpress'的命名實例嗎? –
是的,他們都是這樣。我們將在目標機器上安裝sqlexpress。 –