-1

I am trying to help my user to select a SQL Server instance and a database from a ComboBox. I am already connecting to SQL Server using the following code:

Dim connectionString As String = "Server=DESKTOP-MTHFG8E\PEACRO;" & "Database=my_database;" & "User  ID=my_user_id;" & "Password=my_password;"

Using connection As New SqlConnection(connectionString)
    Try

        connection.Open()
        MsgBox("Connected successfully")

        ' Perform database operations here

    Catch ex As Exception

        MsgBox(ex.Message)

    End Try

End Using

I now want to enable the user to select the server / instance in a ComboBox, and the database in another ComboBox.

rgettman
  • 176,041
  • 30
  • 275
  • 357
Asward
  • 1
  • 2
  • 2
    You will first need to read the available SQL Servers to populate the first ComboBox. Take a look at [this question](https://stackoverflow.com/questions/10781334/how-to-get-list-of-available-sql-servers-using-c-sharp-code). Then, when the user selects an item in the first ComboBox, you can populate the available databases from that server. See [this question](https://stackoverflow.com/questions/13703193/how-to-get-list-of-all-database-from-sql-server-in-a-combobox-using-c-net) for ideas. – Étienne Laneville Apr 18 '23 at 16:56
  • [This](https://www.vbforums.com/showthread.php?532768) may be useful to you. It's main point is something else but it loads that database information in pursuit of that. – jmcilhinney Apr 19 '23 at 00:26

1 Answers1

0

Here is an answer for the adding databases to a combobox part:

Private Property conn As New SqlConnection(sqlConnectionString)
Private servername,username,password as string
Private ReadOnly Property connstring As String
        Get
            Return "Server=" + servername + ";Database=master ;User Id=" + username + ";Password=" + password + ";Connection Timeout=5;"
        End Get
    End Property

'put a value into servername and username and password'
try
 conn.ConnectionString = connstring
                conn.Open()
                If conn.State = ConnectionState.Open Then

                    Using cmd As New SqlCommand With {
                                .CommandType = CommandType.Text,
                                .CommandText = "SELECT name FROM sys.databases WHERE name NOT IN ('master', 'tempdb', 'model', 'msdb'); ",
                                .Connection = conn,
                                .CommandTimeout = 1}

                        Dim sdr As SqlDataReader = cmd.ExecuteReader()
                        Do While sdr.Read()
                            Combobox1.Items.Add(sdr.GetString(0))

                        Loop
                        sdr.Close()
                    End Using
                    conn.Close()


catch ex as exception
end try

I hope this code helps you in the adding databases to a combox part, our combobox name here is Combobox1.

Zayd
  • 21
  • 7
  • Your answer only contains code. I recommend that you don't only post code as an answer, but _also_ provide an explanation about what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. Can you [edit] your answer to include this? – Jeremy Caney Apr 23 '23 at 18:39
  • Hey @JeremyCaney i think i explained that my code helps with adding databases to a combobox, did you mean adding comments to my code? – Zayd Apr 24 '23 at 07:15