数据网格视图不显示mysql表中的数据

本文关键字:数据 mysql 数据网 网格 视图 显示 | 更新日期: 2023-09-27 18:00:58

我试图在数据网格视图中获取数据。如果我使用这个查询,这个代码工作得很好:-"Select*fromtemployee.transaction">

但当我试图设置条件时,它不会给出任何输出(只是显示一个空白表(

我的表有Int.Im类型的月份、年份列,使用mysql服务器5.6.25

我找不到我的代码有问题。请帮忙。提前Thx。

private void load_data_Click(object sender, EventArgs e)
        {

            string constring = "datasource = localhost;port = 3306;username = ****;password = ****";
            MySqlConnection conDataBase = new MySqlConnection(constring);
            var cmdDataBase = conDataBase.CreateCommand();
            cmdDataBase.CommandText = @"select * from employee.transaction where department = @department AND month = @month AND year = @year";
            cmdDataBase.Parameters.AddWithValue("@department", this.department.Text);
            cmdDataBase.Parameters.AddWithValue("@month", this.dateTimePicker1.Value.Month);
            cmdDataBase.Parameters.AddWithValue("@year", this.dateTimePicker1.Value.Year);
         try
            {
         // here im trying to show table in datagrid view
               MySqlDataAdapter sda = new MySqlDataAdapter();
                sda.SelectCommand = cmdDataBase;
                DataTable dbdataset = new DataTable();
                sda.Fill(dbdataset);
                BindingSource bSource = new BindingSource();
                bSource.DataSource = dbdataset;
                dataGridView1.DataSource = bSource;
                sda.Update(dbdataset);
        //here im trying to make a excel file which would contain what is currently being displayed in the datagrid view
                DataSet ds = new DataSet("New_DataSet");
                DataTable dt = new DataTable("New_DataTable");
                dt.Locale = System.Threading.Thread.CurrentThread.CurrentCulture;
                ds.Locale = System.Threading.Thread.CurrentThread.CurrentCulture;
                ds.Tables.Add(dbdataset);
                ExcelLibrary.DataSetHelper.CreateWorkbook("MyExcelFile.xls", ds);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

数据网格视图不显示mysql表中的数据

正如@Bjorn Roger在评论中提到的那样,问题可能是"month"answers"year"都是SQL中的关键字。

我建议你试试:

select * from employee.transaction where department = @department AND [month] = @month AND [year] = @year

第页。S: 请注意[]与字段"month"answers"year"一起使用。

编辑1

此外,您可能需要检查输入字段"月"answers"年"的日期格式是否与数据库表字段的日期格式相同。