Hi Guys,
I'm working Media Database application for my music collection. I currently have multiple versions of the database created with different versions of SQL Server Express. My reasons for this are NOT relevant to the problem I'm trying to solve so please do NOT ask. I have a very simple app that I use to import album cover images into the database and export them back out again as files to ensure that the files were correctly stored. Currently this app is targeted at only one of my databases. I would like to add code to the app that allows me to display a list of all my server instances in a combo box. Once I select an instance I would like to populate a second combo box with a list of databases that are present for that instance, select my database and connect to it so I can perform my import/exports on any one of the databases I choose. I have found some code that is supposed to accomplish this. I am able to retrieve a list of servers but I cannot seem to retrieve the databases for a given instance. I am not sure where the problem and I would appreciate any help you can provide. Here's my code
I'm working Media Database application for my music collection. I currently have multiple versions of the database created with different versions of SQL Server Express. My reasons for this are NOT relevant to the problem I'm trying to solve so please do NOT ask. I have a very simple app that I use to import album cover images into the database and export them back out again as files to ensure that the files were correctly stored. Currently this app is targeted at only one of my databases. I would like to add code to the app that allows me to display a list of all my server instances in a combo box. Once I select an instance I would like to populate a second combo box with a list of databases that are present for that instance, select my database and connect to it so I can perform my import/exports on any one of the databases I choose. I have found some code that is supposed to accomplish this. I am able to retrieve a list of servers but I cannot seem to retrieve the databases for a given instance. I am not sure where the problem and I would appreciate any help you can provide. Here's my code
GetServers:
Sub GetServers()
Dim serverTable As DataTable = SqlDataSourceEnumerator.Instance.GetDataSources()
Dim upperBound As Integer = serverTable.Rows.Count - 1
Dim serverNames(upperBound) As String
For index As Integer = 0 To upperBound
If serverTable.Rows(index).IsNull("InstanceName") Then
serverNames(index) = CStr(serverTable.Rows(index)("ServerName"))
Else
serverNames(index) = String.Format("{0}\{1}", _
serverTable.Rows(index)("ServerName"), _
serverTable.Rows(index)("InstanceName"))
End If
Next
With cmbServers
.BeginUpdate()
.Items.Clear()
.Items.AddRange(serverNames)
.SelectedItem = currentServerName
.Text = currentServerName
.EndUpdate()
End With
End Sub
GetServerDatabases:
Sub GetServerDatabases()
Dim builder As New SqlConnectionStringBuilder()
Using connection As New SqlConnection(GetConnectionString(False))
Try
connection.Open()
cmbDatabases.DataSource = connection.GetSchema("Databases")
builder.IntegratedSecurity = chkIntegratedSecurity.Checked
builder.UserID = txtUserID.Text
builder.Password = txtPassword.Text
If GetConnectionString(True) Then
builder.InitialCatalog = cmbDatabases.Text
End If
Catch
MessageBox.Show("Unable to connect.", _
"Connection Error", _
MessageBoxButtons.OK, _
MessageBoxIcon.Error)
End Try
End Using
End Sub
GetConnectionString:
Private Function GetConnectionString(ByVal includeDatabase As Boolean) As String
Dim csbbuilder As New SqlConnectionStringBuilder()
csbbuilder.DataSource = cmbServers.Text
If includeDatabase Then
csbbuilder.InitialCatalog = cmbDatabases.Text
End If
Return csbbuilder.ConnectionString
End Function