0

我们将更改 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
4

3 回答 3

2

或者更加神秘。

strComputerName = Environment.MachineName
于 2012-06-08T11:19:03.623 回答
1

由于您使用的是 VB.net,因此您可以访问My命名空间,这使得这非常容易。

strComputerName = My.Computer.Name

任务完成。

于 2012-06-08T11:14:52.350 回答
1

您也可以尝试以下任何一种代码行。这里Data Source=.;表示来自本地计算机的数据库

strConnectionString = "Data Source=.\sqlexpress;Integrated Security=True;User Instance=True"

- 或者 -

strConnectionString = "Data Source=.;Integrated Security=True;User Instance=True"
于 2012-06-08T11:44:21.243 回答