簡體   English   中英

數據網格視圖未顯示 mysql 表中的數據

[英]Data grid view not showing data from mysql table

我試圖在數據網格視圖中獲取數據。 如果我使用此查詢,此代碼工作正常:-“從employee.transaction中選擇*”

但是當我嘗試設置條件時,它沒有給出任何輸出(只顯示一個空白表)

我的表有使用 mysql 服務器 5.6.25 的 Int.Im 類型的月份、年份列

我找不到我的代碼的問題。請幫忙。提前謝謝。

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

正如@Bjorn-Roger 在評論中所提到的,問題可能是“月”和“年”都是 SQL 中的關鍵字。

我建議你嘗試:

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

PS:注意將 [] 與字段“月”和“年”一起使用。

編輯 1

此外,您可能想要檢查輸入字段“月”和“年”的日期格式是否與數據庫表字段的日期格式相同。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM