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
2
  • Did you try a . or (local)? Also are you sure every single computer will have a named instance called sqlexpress? Commented Jun 8, 2012 at 12:18
  • Yes, all of them have it that way. We will be installing sqlexpress on the target machines. Commented Jun 8, 2012 at 13:30

3 Answers 3

2

Or the far more arcane.

strComputerName = Environment.MachineName
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks everyone for your quick responses. My.Computer.Name and Evironment.MachineName both worked. We are going to go with this Environment.MachineName. Which one is the prefered one?
Doesn't really matter. My is available in VB and joins together a bunch of related stuff you might want for say a Help About screen or a OS compatibility check. So you might find My handier if you need some more bits and pieces.
1

Since you're using VB.net, you've got access to the My namespace, which makes this super-easy.

strComputerName = My.Computer.Name

Job done.

Comments

1

You can also try any one of the following line of code. Here Data Source=.; indicates the database from the local computer

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

--OR--

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.