在这里需要帮助,
我需要将标题放入名为cntpaginaBX和prmopaginaBX的组合框。
与一个工作,但当我尝试两者..它失败。
我想知道的是“ MySqlCommand”。
我如何解决它,因为它不包含内容,促销,sqlConn togheter。

        MySqlConnection sqlConn = new MySqlConnection("Database=joshua;
        Data Source=localhost; User Id='root'; Password=''");
        string content = "SELECT title FROM content";
        string promo = "SELECT title FROM promo";
        MySqlCommand myCommando = new MySqlCommand(content, promo, sqlConn); <-----here
        MySqlDataReader sqlReader;
        object cont;
        object prom;

        try
        {
            sqlConn.Open();
            sqlReader = myCommando.ExecuteReader();
            while (sqlReader.Read())
            {
                cont = sqlReader.GetValue(0).ToString();
                cntpaginaBX.Items.Add(cont);
                prom = sqlReader.GetValue(0).ToString();
                prmopaginaBX.Items.Add(prom);
            }

            sqlReader.Close();
        }
        catch (Exception x)
        {
            MessageBox.Show(x.Message, "Fout", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        finally
        {
            sqlConn.Close();
        }

最佳答案

MySqlCommand用于运行单个查询。您不能简单地同时运行两个查询。每个查询都必须是完全独立的MySqlCommand

string content = "SELECT title FROM content";
MySqlCommand contentCommand = new MySqlCommand(content, sqlConn);
try
{
    sqlConn.Open();
    sqlReader = contentCommand.ExecuteReader();
    while (sqlReader.Read())
    {
        cont = sqlReader.GetValue(0).ToString();
        cntpaginaBX.Items.Add(cont);
    }

    sqlReader.Close();
}


接着:

string promo = "SELECT title FROM promo";
MySqlCommand promoCommand = new MySqlCommand(promo, sqlConn);
try
{
    sqlConn.Open();
    sqlReader = promoCommand.ExecuteReader();
    while (sqlReader.Read())
    {
            prom = sqlReader.GetValue(0).ToString();
            prmopaginaBX.Items.Add(prom);
    }

    sqlReader.Close();
}

关于c# - MySqlCommand:如何将标题放入组合框,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5842009/

10-09 06:50