显示MySql服务器的数据库

时间:2011-10-16 13:53:24

标签: c# mysql sql database

有谁知道,如何在C#中显示数据库?我知道这可能通过执行sql命令show databases,但我不知道如何配置阅读器。有人请帮帮我。

编辑:我找到了解决方案:

    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);
        }

    }

2 个答案:

答案 0 :(得分:3)

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();

答案 1 :(得分:1)

SqlConnection conn = new SqlConnection(ConnectionString);

SqlCommand com = new SqlCommand ("show databases",conn);
conn.Open();
SqlDataReader reader = com.ExecuteReader();
DataTable dt = new DataTable;
dt.Load(reader);
DataRows[] rows = dt.Rows;

认为您可以查看数据行

那就是说,如果你已经有了连接字符串,就没有理由不打开MSqlServer或其他任何东西并从那里查看...