开发者

MySqlCommand: how to get title into comboboxes

开发者 https://www.devze.com 2023-03-01 13:47 出处:网络
Need help here, i need to get the title into the comboboxes named cntpaginaBX and prmopaginaBX. with one it works but when i try both .. It fails.

Need help here, i need to get the title into the comboboxes named cntpaginaBX and prmopaginaBX. with one it works but when i try both .. It fails. What i want to know is the "MySqlCommand". how do i fix it because it doesnt take content, promo, sqlConn togheter.

        MySqlConnection sqlConn = new MySqlConnection("Database=joshua;
        Data开发者_JAVA百科 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();
        }


A MySqlCommand is used to run a single query. You cannot simply run two queries together. Each query needs to be a completely separate 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();
}

And then:

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

精彩评论

暂无评论...
验证码 换一张
取 消