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

时间:2014-03-11 00:32:50

标签: c# mysql winforms

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

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

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

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的新手,请指教。

1 个答案:

答案 0 :(得分:1)

我认为你有两个错误。首先你应该从查询字符串中删除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;