剛剛開始編碼C#,我是一個絕對的初學者。話雖如此,我有一個關於使用MySqlConnector和MySQL查詢的問題。C#visual studio和多個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());
{
}
}
}
}
}
}
在此先感謝!
這就是我一直在尋找的!謝謝史蒂夫 – Dennis1679
試圖讓這與我想要的工作,但它不工作。當我循環閱讀器。閱讀,它也從第二個查詢字符串的結果,並將其放置在我的列表框。你能幫我嗎?看到我的第一篇文章更新代碼。 – Dennis1679
現在有點忙,你應該保留當前循環的計數器並更新正確的列表 – Steve