gpt4 book ai didi

c# winform 显示组合框选择的数据库数据

转载 作者:行者123 更新时间:2023-11-29 13:09:07 25 4
gpt4 key购买 nike

我有一个复选按钮,可以在组合框中获取月份和年份:

private void cmdSend_Click(object sender, System.EventArgs e)
{
List<string>[] list;
list = dbConnect.Select(month_list.SelectedItem.ToString(), year_list.SelectedItem.ToString());

printer_info.Rows.Clear();
for (int i = 0; i < list[0].Count; i++)
{
int number = printer_info.Rows.Add();
printer_info.Rows[number].Cells[0].Value = list[0][i];
printer_info.Rows[number].Cells[1].Value = list[1][i];
printer_info.Rows[number].Cells[2].Value = list[2][i];
printer_info.Rows[number].Cells[3].Value = list[3][i];
}
}

enter image description here

检查按钮然后将月份和年份传递给选择语句函数:

public List<string>[] Select(string month,string year)
{
string query = "SELECT * FROM page_counter WHERE month = '@month' AND year = @year;";

//Create a list to store the result
List<string>[] list = new List<string>[4];
list[0] = new List<string>();
list[1] = new List<string>();
list[2] = new List<string>();
list[3] = new List<string>();

//Open connection
if (this.OpenConnection() == true)
{
//Create Command
MySqlCommand cmd = new MySqlCommand(query, connection);
cmd.Parameters.Add("@month",MySqlDbType.VarChar);
cmd.Parameters.Add("@year", MySqlDbType.Year);
cmd.Parameters["@month"].Value = month;
cmd.Parameters["@year"].Value = year;

//Create a data reader and Execute the command
MySqlDataReader dataReader = cmd.ExecuteReader();

//Read the data and store them in the list
while (dataReader.Read())
{
list[0].Add(dataReader["id"].ToString() + "");
list[1].Add(dataReader["month"].ToString() + "");
list[2].Add(dataReader["year"].ToString() + "");
list[3].Add(dataReader["page_count"].ToString() + "");
}

//close Data Reader
dataReader.Close();

//close Connection
this.CloseConnection();

//return list to be displayed
return list;
}

数据将显示在 GridView 上,其中所有列在页面设计器中默认指定:
enter image description here

当我运行代码时,它没有任何错误,但 GridView 上没有显示任何值。我有什么错误吗?我是c# winform新手,请指教。

最佳答案

我认为你有两个错误。首先你应该删除 single-quotes来自查询字符串:

string query = "SELECT * FROM page_counter WHERE month = @month AND year = @year;"

因为当您使用单引号时,您的参数名称将被视为实际值。其次,我强烈建议您为您的项目使用类,而不是 List<string>[] .类看起来像这样:

public class Data
{
public int Id { get; set; }
public string Month { get; set; }
public string Year { get; set; }
public int PageCount { get; set; }
}

然后创建一个List<Data>并像这样填充它:

 var dataList = new List<Data>();
while (dataReader.Read())
{
var item = new Data();
item.Id = Convert.Toınt32(dataReader["id"]);
item.Month = dataReader["month"].ToString();
item.Year = dataReader["year"].ToString();
item.PageCount = Convert.ToInt32(dataReader["page_count"]);
dataList.Add(item);
}
return dataList;

然后当然要更改方法的返回类型:

public List<Data> Select(string month,string year)

那么您需要做的就是设置 DataSource属性:

var list = dbConnect.Select(month_list.SelectedItem, year_list.SelectedItem);
printer_info.DataSource = list;

关于c# winform 显示组合框选择的数据库数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22314147/

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