MySQL服务器的数据库显示 [英] Show databases of MySql server

查看:135
本文介绍了MySQL服务器的数据库显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人是否知道,如何显示在C#中的数据库?我知道通过执行SQL命令显示数据库这就是可能的,但我不知道如何配置读卡器。有人请帮助我。



修改:我发现了一个解决方案:

 私人无效Window_Loaded(对象发件人,RoutedEventArgs E)
{
的MySqlConnection CON =新的MySqlConnection(this.constr);
的MySqlCommand CMD = con.CreateCommand();
cmd.CommandText =显示数据库;

{
con.Open();
MySqlDataReader将读卡器= cmd.ExecuteReader();
,而(reader.Read())
{
串行=;
的for(int i = 0; I< reader.FieldCount;我++)
行+ = reader.GetValue(I)的ToString();
listBox1.Items.Add(行);
}


}
赶上(个MySqlException前)
{
MessageBox.Show(ex.Number.ToString());
MessageBox.Show(ex.Message);
}

}


解决方案

 字符串myConnectionString =SERVER =本地主机; UID ='根'; +PASSWORD ='根';; 
的MySqlConnection连接=新的MySqlConnection(myConnectionString);
的MySqlCommand命令= connection.CreateCommand();
command.CommandText =SHOW数据库;;
了MySqlDataReader阅读器;
connection.Open();
=读者Command.ExecuteReader却();
,而(Reader.Read())
{
串行=;
的for(int i = 0; I< Reader.FieldCount;我++)
行+ = Reader.GetValue(I)的ToString()+,;
comboBox1.Items.Add(行);
}
的Connection.close();


Does anybody know, how to show databases in C#? I know thats possible by executing a sql command show databases, but i dont know how to configure reader. Anybody please help me.

EDIT: I found a solution:

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        MySqlConnection con = new MySqlConnection(this.constr);
        MySqlCommand cmd = con.CreateCommand();
        cmd.CommandText = "show databases";
        try
        {
            con.Open();
            MySqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                string row = "";
                for (int i = 0; i < reader.FieldCount; i++)
                    row += reader.GetValue(i).ToString();
                listBox1.Items.Add(row);
            }


        }
        catch (MySqlException ex)
        {
            MessageBox.Show(ex.Number.ToString());
            MessageBox.Show(ex.Message);
        }

    }

解决方案

string myConnectionString = "SERVER=localhost;UID='root';" + "PASSWORD='root';";
MySqlConnection connection = new MySqlConnection(myConnectionString);
MySqlCommand command = connection.CreateCommand();
command.CommandText = "SHOW DATABASES;";
MySqlDataReader Reader;
connection.Open();
Reader = command.ExecuteReader();
while (Reader.Read())
{
  string row = "";
  for (int i = 0; i < Reader.FieldCount; i++)
       row += Reader.GetValue(i).ToString() + ", ";
       comboBox1.Items.Add(row);
}
connection.Close();

这篇关于MySQL服务器的数据库显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆