0

We are going to change the connection string in Settings.vb so we don't need to worry about what it is when our app runs on a different computer other than the development computer.

Our code looks like this:

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

Can you tell me how to obtain the computer name because we need that information to place into the "Data Source=" part of the connection string?

Update: Here is what the final coding looks like. Thanks everyone for your replies:

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

次のコード行のいずれかを試すこともできます。ここで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 に答える
1

VB.net を使用しているため、My名前空間にアクセスできるため、これが非常に簡単になります。

strComputerName = My.Computer.Name

ジョブ完了。

于 2012-06-08T11:14:52.350 に答える