Jump to content

Displaying a list of Databases in a combo box in VB.NET


Recommended Posts

Hi guys,

Ive a simple question for you all (well what i hope is a simple question :) )

Lets say I have a msql server with three databases created i.e. test, test_1 and test_3. Is there a way that I can display these three databases in a combo box? I want the user to be able to select which database he/she wishes to connect to. Im using VB.NET for this and any help would be really cool :)

Thanks in advance.

Link to comment
Share on other sites


I quicky dit it in a Asp.Net / Vb.Net project:

    code for the page:
<div>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1"
DataTextField="name" DataValueField="name">
</asp:DropDownList><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [name] FROM [master].[dbo].[sysdatabases] Order By [name]"></asp:SqlDataSource>
</div>

code for the web config:

<connectionStrings>
<add name="NorthwindConnectionString" connectionString="Data Source=SERVER2003;Initial Catalog=master;User ID=sa;Password=mysapassword"
providerName="System.Data.SqlClient" />
</connectionStrings>

Link to comment
Share on other sites

Thanks for the help guys, I figured it out below is a snippet of the code:

However now my problem is that I cant figure out a way to allow the user to connect to the database which they just selected, any ideas??

Note: The code is written very poorly :blink:



Dim conn As MySqlConnection
Dim myCommand As New MySqlCommand
Dim myAdapter As New MySqlDataAdapter
Dim dataset As New DataSet
Dim SQL As String
Dim myData As New DataTable

conn = New MySqlConnection
conn.ConnectionString = "server=*******;" & "user id=" & usernametxt.Text & ";" _
& "password=" & pwdtxt.Text & ";"
Try
conn.Open()
MessageBox.Show("Connection Opened Successfully!")
Catch myerror As MySqlException
MessageBox.Show("Error was encountered when connecting" & myerror.Message)
Finally

End Try


SQL = "Show Databases;"
myCommand.Connection = conn
myCommand.CommandText = SQL
myAdapter.SelectCommand = myCommand
myAdapter.Fill(myData)
comboBox.DataSource = myData
comboBox.DisplayMember = "Database"
conn.Close()


Edited by mstester
Link to comment
Share on other sites

  • 3 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...