【问题标题】:How I list table name in mysql into combobox using C#?如何使用 C# 将 mysql 中的表名列出到组合框中?
【发布时间】:2015-07-31 07:00:12
【问题描述】:

我想将 mysql 数据库中的表名列出到组合框 winform C# 中。 我在 phpmyadmin 中使用这个 SQL 查询,它会返回我想要的结果。

SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE '%nh%'

然后我尝试用 C# 编写函数。这是我的代码

void get_table()
    {
        string myConnection = "Server=localhost;Database=sctv_data;Port=3306;User ID=root;Password=;Charset=utf8";
        string query = "SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE '%nh%'";
        conDatabase = new MySqlConnection(myConnection);
        MySqlCommand cmdDataBase = new MySqlCommand(query, conDatabase);
        MySqlDataReader myReader;
        try
        {
            conDatabase.Open();
            myReader = cmdDataBase.ExecuteReader();
            cb_data_name.Items.Clear();
            if (myReader.HasRows == true)
            {
                while (myReader.Read())
                {
                    cb_chon.Items.Add((string)myReader[0]);
                }
            }
        }
        catch
        {

        } 
    }

但它失败了;我认为它有问题,但我不知道如何解决。

【问题讨论】:

  • 你说的失败是什么意思?您收到任何异常或错误消息?
  • 您遇到了什么异常?你的 catch 块是邪恶的。 Catch 块旨在捕获异常而不是默默地吞下它们。参考这个Link

标签: c# mysql winforms


【解决方案1】:

使用SqlDataAdapter

void get_table()
{
    string myConnection = "Server=localhost;Database=sctv_data;Port=3306;User ID=root;Password=;Charset=utf8";
    string query = "SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE '%nh%'";
    conDatabase = new MySqlConnection(myConnection);
    MySqlCommand cmdDataBase = new MySqlCommand(query, conDatabase);
    MySqlDataAdapter myAdapter;
    try
    {
        conDatabase.Open();
        myAdapter= new mySqlAdapter(cmdDataBase);
        cb_data_name.Items.Clear();
        DataTable dt=new DataTable();
        mySqlAdapter.Fill(dt);
        cb_data_name.DatSource=dt;
        cb_data_name.DisplayMemeber="table_name";
    }
    catch
    {

    } 
}

【讨论】:

  • 对不起,但似乎" string myConnection = "Server=localhost;Database=sctv_data;Port=3306;User ID=root;Password=;Charset=utf8";"没有效果,似乎列出了mysql中的所有数据表@Ullas
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-16
  • 2021-11-26
  • 1970-01-01
  • 1970-01-01
  • 2015-07-20
  • 2010-10-10
相关资源
最近更新 更多