gpt4 book ai didi

C# Mysql 多重查询

转载 作者:行者123 更新时间:2023-11-29 01:21:19 25 4
gpt4 key购买 nike

我正在尝试构建一个小的状态工具。我需要获得多个查询的结果(大约 4-5 个)。一般连接设置和“如何读取数据”已经完成,但我无法弄清楚另一个查询是如何执行的。

我在搜索它时发现的所有内容都是针对 SqlClient 的。我对此完全负担过重。

到目前为止,这是我的代码(请耐心等待,我是新手):

private void button1_Click(object sender, EventArgs e)
{
if(listView1.Items.Count > 1)
{
listView1.Items.Clear();
}
var listMember = new List<string>{};
var listOnline = new List<string>{};

// SQL PART //
string connString = "Server=10*****;Port=3306;Database=e***;Uid=e***;password=********************;";
MySqlConnection conn = new MySqlConnection(connString);
MySqlCommand command = conn.CreateCommand();
command.CommandText = "SELECT fullname,online FROM member WHERE active = '1' ORDER BY online DESC";
try
{
conn.Open();
}
catch (Exception ex)
{
listView1.Items.Add("Error: " + ex);
}
MySqlDataReader reader = command.ExecuteReader();
while(reader.Read())
{
listMember.Add(reader["fullname"].ToString());
listOnline.Add(reader["online"].ToString());
}
conn.Close();
// SQL ENDING //

// SET ENTRIES TO LISTVIEW //
int counter = 0;
foreach(string member in listMember)
{
ListViewItem item = new ListViewItem(new[] { member, listOnline.ElementAt(counter) });
item.ForeColor = Color.Green;
listView1.Items.Add(item);

counter++;
}
}

我不太确定设计/布局最终会是什么样子,所以我想将结果附加到 sql-part 中的列表,以便稍后处理列表中的数据。

我真的必须在 conn.Close() 之后设置一个完整的新连接吗?或者还有其他办法吗?我可以想象:5 个查询有自己的连接、尝试、捕获和 2 个循环……这将得到大约 100-200 行,只是为了从 5 个查询中获取结果。这么简单的事情,是不是有点过分了?

希望得到一些帮助。问候。

根据新评论我的最新代码:

顶部:

public partial class Form1 : Form
{
public static string connString = "Server=10****;Port=3306;Database=e****;Uid=e****;password=****;";
public Form1()
{
InitializeComponent();
MySqlConnection conn = new MySqlConnection(connString); // Error gone!
}

正文部分:

 public void QueryTwoFields(string s, List<string> S1, List<string> S2)
{
try
{
MySqlCommand cmd = conn.CreateCommand(); // ERROR: conn does not exist in the current context.
cmd.CommandType = CommandType.Text;
string command = s;
cmd.CommandText = command;
MySqlDataReader sqlreader = cmd.ExecuteReader();
while (sqlreader.Read())
{
S1.Add(sqlreader[0].ToString());
S2.Add(sqlreader[1].ToString());
}
sqlreader.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

private void button1_Click(object sender, EventArgs e)
{
if(listView1.Items.Count > 1)
{
listView1.Items.Clear();
}
var listMember = new List<string>{};
var listOnline = new List<string>{};

using (conn) // ERROR: conn does not exist in the current context.
{
conn.Open();
///...1st Query
QueryTwoFields("SELECT fullname,online FROM member WHERE active = '1' ORDER BY online DESC",listMember,listOnline);
//...2nd query
//QueryTwoFields("your new Select Statement", otherList, otherList);
}
}

最佳答案

您不必在每次执行一个查询时都关闭连接,而不必关闭分配给该连接的 sqlreader。最后,当所有查询都已执行后,您将关闭连接。还可以考虑使用 using:

您还可以定义一个方法来执行您的查询,以使您的代码不重复:

    public  void QueryTwoFields(string s, List<string> S1, List<string> S2)    
///Select into List S1 and List S2 from Database (2 fields)
{
try
{
MySqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
string command = s;
cmd.CommandText = command;
MySqlDataReader sqlreader = cmd.ExecuteReader();
while (sqlreader.Read())
{
S1.Add(sqlreader[0].ToString());
S2.Add(sqlreader[1].ToString());
}
sqlreader.Close();

}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void button1_Click(object sender, EventArgs e)
{
if(listView1.Items.Count > 1)
{
listView1.Items.Clear();
}
var listMember = new List<string>{};
var listOnline = new List<string>{};

// SQL PART //

using (conn)
{
conn.Open();
///...1st Query
QueryTwoFields("SELECT fullname,online FROM member WHERE active = '1' ORDER BY online DESC",listmember,listonline)
//...2nd query
QueryTwoFields("your new Select Statement",myOtherList1,myOtherlist2)
....
}
}

编辑:请记住,您不能在按钮处理程序中定义 QueryTwoFields 方法。您必须在外部定义它(请参阅上面的代码)。还要在程序的开头定义您的连接数据:

    namespace MyProject
{
/// <summary>
/// Defiine your connectionstring and connection
/// </summary>
///

public partial class Form1 : Form
{ public static string connString = "Server=10*****;Port=3306;Database=e***;Uid=e***;password=********************;";
MySqlConnection conn = new MySqlConnection(connString);

.........

关于C# Mysql 多重查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25659631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com