刚开始编码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());
{
}
}
}
}
}
}
提前致谢!
答案 0 :(得分:3)
您可以在查询文本中放置多个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());
}
}
答案 1 :(得分:1)
使用功能。
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();
}
答案 2 :(得分:0)
为每个附加查询初始化一个新的MySqlCommand。
MySqlCommand command2 = connection.CreateCommand();
command2.CommandText = ""; // here the new query