It shows how to list the MySql database into the listbox using C#.NET.
Keywords: Listbox, database, Mysql, Sql, CSharp.NET, .NET.
To list of Databases from MySql
private void getDatabase()
{
//Declaration
OdbcConnection
conOdbc = new OdbcConnection();
DataTable
dt = new DataTable();
//Connection
String
strMySQL = "";
strMySQL = "Driver={MySQL
ODBC 3.51 Driver};option=0;port=3306;server=
localhost;uid=root;password=root";
myChk = "";
myChk = strMySQL;
//Open
Odbc Connection
conOdbc = new
OdbcConnection(myChk);
conOdbc.Close();
conOdbc.Open();
selectQuery = "SELECT DISTINCT TABLE_SCHEMA FROM
information_schema.TABLES ";
OdbcDataAdapter
odbcAdptr = new OdbcDataAdapter(selectQuery,
conOdbc);
odbcAdptr.Fill(dt);
dtseCt = 0;
cmbDatabase.Items.Clear();
cmbDatabase.Items.Add("---Select---");
while
(dtseCt < dt.Rows.Count)
{
cmbDatabase.Items.Add(dt.Rows[dtseCt][0].ToString());
dtseCt = dtseCt + 1;
}
cmbDatabase.SelectedIndex = 0;
}
Here the Complete Code.
using System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Text;
using
System.Windows.Forms;
using
Microsoft.VisualBasic;
using
System.Diagnostics;
using System.IO;
using
System.Data.Odbc;
namespace
WindowsApplication1
{
public partial class Form1 : Form
{
string
strMySQL = null;
string
myChk = null;
string
selectQuery = null;
int
dtseCt = 0;
public
Form1()
{
InitializeComponent();
}
private
void Form1_Load(object
sender, EventArgs e)
{
getDatabase();
}
private
void getDatabase()
{
//Declaration
OdbcConnection
conOdbc = new OdbcConnection();
DataTable dt = new
DataTable();
//Connection
String
strMySQL = "";
strMySQL = "Driver={MySQL
ODBC 3.51 Driver};option=0;port=3306;server=
localhost;uid=root;password=root";
myChk = "";
myChk = strMySQL;
//Open
Odbc Connection
conOdbc = new
OdbcConnection(myChk);
conOdbc.Close();
conOdbc.Open();
selectQuery = "SELECT DISTINCT TABLE_SCHEMA FROM
information_schema.TABLES ";
OdbcDataAdapter
odbcAdptr = new OdbcDataAdapter(selectQuery,
conOdbc);
odbcAdptr.Fill(dt);
dtseCt = 0;
cmbDatabase.Items.Clear();
cmbDatabase.Items.Add("---Select---");
while
(dtseCt < dt.Rows.Count)
{
cmbDatabase.Items.Add(dt.Rows[dtseCt][0].ToString());
dtseCt = dtseCt + 1;
}
cmbDatabase.SelectedIndex = 0;
}
}
}
Note: If you have any suggestions or any new topics please inform us at sivodayatech@dotnetdevelopertool.com