【问题标题】:C# visual studio and multiple mySQL query'sC# Visual Studio 和多​​个 mySQL 查询
【发布时间】:2016-03-05 22:31:42
【问题描述】:

刚开始编写 C# 代码,我绝对是它的初学者。话虽如此,我有一个关于使用 MySqlConnector 和 mysql 查询的问题。

我目前有一个查询,我用它填充了一个列表框的结果。但我喜欢做的是向阅读器添加更多查询,并将其他查询的结果放在组合框中。那么我该怎么做呢?我搜索了谷歌并没有找到答案。

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace Badminton_stand
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void timer1_Tick(object sender, EventArgs e)
        {
            labelCurrentTime.Text = DateTime.Now.ToString();
        }



        private void Form1_Load(object sender, EventArgs e)
        /*  {
              string MyConString = "Server=localhost;Port=3307;Database=database1;UID=root;Password=toor";
              MySqlConnection connection = new MySqlConnection(MyConString);
              MySqlCommand command = connection.CreateCommand();
              MySqlDataReader Reader;
              command.CommandText = @"select Club from teams;
                  select team from teams";
              connection.Open();
              Reader = command.ExecuteReader();
              while (Reader.Read())
              {
                  string thisrow = "";
                  for (int i = 0; i < Reader.FieldCount; i++)
                      thisrow += Reader.GetValue(i).ToString() + " ";
                  clubSelectorBox1.Items.Add(thisrow);
              }
              while (Reader.NextResult()) ;
              {
                  string thisrow = "";
                  for (int i = 0; i < Reader.FieldCount; i++)
                      thisrow += Reader.GetValue(i).ToString() + " ";
                  cbTeam1.Items.Add(thisrow); 
              }
              connection.Close();
          } */
        {
            string cmdText = @"SELECT Club from teams;
                   SELECT team from teams";
            using (MySqlConnection cnn = new MySqlConnection("Server=localhost;Port=3307;Database=badminton;UID=root;Password=usbw"))
            using (MySqlCommand cmd = new MySqlCommand(cmdText, cnn))
            {
                cnn.Open();
                using (MySqlDataReader reader = cmd.ExecuteReader())
                {
                    do
                    {
                        while (reader.Read())
                        {
                            string thisrow = "";
                            for (int i = 0; i < reader.FieldCount; i++)
                                thisrow += reader.GetValue(i).ToString() + " ";
                            clubSelectorBox1.Items.Add(thisrow);
                        }

                    }
                    while (reader.NextResult());
                    {

                    }
                }
            }

        }
    }
}

提前致谢!

【问题讨论】:

    标签: c# mysql winforms


    【解决方案1】:

    您可以在查询文本中放置多个 SELECT 语句,并使用 MySqlDataReader 的 NextResult 方法循环遍历结果。

    这是一个如何做的例子,当然你应该适应你的数据和控件

    string cmdText = @"SELECT * from TABLE_A;
                       SELECT * from TABLE_B";
    using(MySqlConnection cnn = new MySqlConnection(......))
    using(MySqlCommand cmd = new MySqlCommand(cmdText, cnn))
    {
        int curTable = 0;
        cnn.Open();
        using(MySqlDataReader reader = cmd.ExecuteReader())
        {
            do
            {
                while (reader.Read())
                {
                    if(curTable == 0)
                        Console.WriteLine("Update first list based on the first table");
                    else if(curTable == 1)
                        Console.WriteLine("Update second list based on second table");
                }
                Console.WriteLine("Go to next result");
                curTable++;
            }
            while(reader.NextResult());
        }
    }
    

    【讨论】:

    • 这就是我要找的!谢谢史蒂夫
    • 试图让它与我想要的一起工作,但它不起作用。当我遍历 reader.Read 时,它还会从第二个查询字符串中获取结果并将其放在我的 listBox 中。你能帮帮我吗?有关更新的代码,请参阅我的第一篇文章。
    • 现在有点忙,你应该保留当前循环的计数器并更新正确的列表
    【解决方案2】:

    使用函数。

     private void Form1_Load(object sender, EventArgs e)
            {
                LoadCombo(clubSelectorbox1, "select club from teams");
                //now just change the control and the query as needed
                LoadCombo(clubSelectorbox2, "select club from teams");
            }
    
     private function LoadCombo(DropDownControl myCombo, string query){
    string MyConString = "Server=localhost;Port=3307;Database=database1;UID=root;Password=toor";
                MySqlConnection connection = new MySqlConnection(MyConString);
                MySqlCommand command = connection.CreateCommand();
                MySqlDataReader Reader;
                command.CommandText = query; // use query here
                connection.Open();
                Reader = command.ExecuteReader();
                while (Reader.Read())
                {
                    string thisrow = "";
                    for (int i = 0; i < Reader.FieldCount; i++)
                        thisrow += Reader.GetValue(i).ToString() + " ";
    
                    myCombo.Items.Add(thisrow); //use myControl here
                }
                connection.Close();
           }
    

    【讨论】:

      【解决方案3】:

      为每个附加查询初始化一个新的 MySqlCommand。

      MySqlCommand command2 = connection.CreateCommand();
      command2.CommandText = ""; // here the new query
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-05-02
        • 2011-05-22
        • 1970-01-01
        • 1970-01-01
        • 2014-10-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多