how will i change that connection/datasource as what the user wants?
I thought you did not want them to be able to change the DataSource.
If you need to allow them to select a server, maybe this will work for you?
Dim csb As New SqlClient.SqlConnectionStringBuilder
csb.IntegratedSecurity = True
csb.UserInstance = True
' Full File Path Works with both these DataSource constructs
'csb.AttachDBFilename = "F:\SQLServer2008\AttachedDBTest.mdf"
' DataSource = ".\" & InstanceName
' DataSource = ServerName & "\" & InstanceName
' |DataDirectory| only works with DataSource = ".\" & InstanceName
csb.AttachDBFilename = "|DataDirectory|\AttachedDBTest.mdf"
' Try to retrieve the available servers
Dim dtSQLServers As DataTable = System.Data.Sql.SqlDataSourceEnumerator.Instance.GetDataSources()
If dtSQLServers.Rows.Count > 0 Then
Try
Dim ServerName As String = dtSQLServers.Rows.Item(0).Field(Of String)("ServerName")
Dim InstanceName As String = dtSQLServers.Rows.Item(0).Field(Of String)("InstanceName")
csb.DataSource = ".\" & InstanceName
'csb.DataSource = ServerName & "\" & InstanceName
Catch ex As Exception
MsgBox(ex.Message)
Exit Sub
End Try
End If
Dim conn As New SqlClient.SqlConnection(csb.ConnectionString)
conn.Open()
conn.Close()
TnTinMN
Practically a Master Poster
640 posts since Jun 2012
Reputation Points: 418
Solved Threads: 148
Skill Endorsements: 13
The code I posted showed you a way to retrieve the available instances of SQL Server within the local network [System.Data.Sql.SqlDataSourceEnumerator.Instance.GetDataSources()].
Where you place the code is up to you to decide.
Personally, I would make the user select a server on the first time execution of the program and then store that server info as a user setting. For subsequent runs, do a check on load to verify that the server is still available. If not, prompt the user for a new selection. It would also be advisable to verify that the database file exists where you expect it to be before attempting to make a connection. Users do get curious at times and can delete your files.
TnTinMN
Practically a Master Poster
640 posts since Jun 2012
Reputation Points: 418
Solved Threads: 148
Skill Endorsements: 13
Curious is not the word I use when that happens!
LOL. And just what may the word you use be?

TnTinMN
Practically a Master Poster
640 posts since Jun 2012
Reputation Points: 418
Solved Threads: 148
Skill Endorsements: 13
Question Answered as of 3 Months Ago by
john.knapp
and
TnTinMN