c# - 如何根据组合框中的选定值从数据库中检索数据?

标签 c# mysql sql

我是 c# 的新手。我有一个允许用户选择月份和日期的组合框代码。当用户单击 cmdSend 按钮时,程序将检索月份和日期表单组合框并调用 dbConnect.Select 类函数来执行 select mysql 语句。

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

        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];
        }
    }  

检索数据库类:

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);
            //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;
        }
        else
        {
            return list;
        }
    }  

但是这段代码不起作用,有人可以告诉我吗?

编辑:

string query = "SELECT * FROM page_counter where month = @month  AND year = @year;";
//Open connection
if (this.OpenConnection() == true)
{
//Create Command
 MySqlCommand cmd = new MySqlCommand(query, connection);
 cmd.Parameters.AddWithValue("@month",month);
 cmd.Parameters.AddWithValue("@year",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();

我已经按照建议编辑了代码,但是我在 AddWithValue 上有一个错误,它说:不包含 AddWithValue 的定义并且没有扩展方法 AddWithValue,我已经添加了 Data.MySqlClient 引用但仍然保持不变。请指教。

最佳答案

问题 1:您需要使用组合框的 SelectedItem 属性从中获取所选项目。

解决方案一:

替换这个:

list = dbConnect.Select(month_list.Text, year_list.Text);

有了这个:

list = dbConnect.Select(month_list.SelectedItem.ToString(),        
                         year_list.SelectedItem.ToString());

问题 2:

我相信您的 Database 中的 MonthYear 列是 INT 列。如果它们是 INT 列,您不需要将 monthyear 参数值括在单引号内。

解决方案 2:

试试这个:

string query = "SELECT * FROM page_counter where month =  
                   " + month + " AND year =" + year + " ;";

建议:您的查询对 sql 注入(inject)攻击开放,我建议使用参数化查询来避免它们。

尝试使用参数化查询:

string query = "SELECT * FROM page_counter where month = @month  AND year = @year;";
//Open connection
if (this.OpenConnection() == true)
{
  //Create Command
  MySqlCommand cmd = new MySqlCommand(query, connection);
  cmd.Parameters.AddWithValue("@month",month);
  cmd.Parameters.AddWithValue("@year",year );

       //Remaining same

        //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();

关于c# - 如何根据组合框中的选定值从数据库中检索数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22058628/

相关文章:

c# - ManualResetEventSlim.Set() 并不总是解锁任务内的等待

C# 与 VB.Net - 隐式转换

mysql - 我的 mysql 查询根据多个子句返回多个总计,引发异常

mysql - SQL可以在日期列中显示(仅显示)非日期字符串吗?

mysql - 统计记录中有 2 个特定里程碑值的用户

sql - 将查询从 Firebird 转换为 PostgreSQL

c# - 在运行时调整控件大小

javascript - 将 jQuery $.when 和 $.then 与两个函数一起使用

php - MySQL 代码导致 PHP 脚本在 popen/exec 崩溃

sql - PostgreSQL:使用 psql 命令行实用程序时 Windows 上的编码问题