2015-07-04 152 views
-1

我試圖在數據網格視圖中獲取數據。此代碼工作正常,如果我用這個查詢: - 「選擇從employee.transaction *」數據網格視圖不顯示來自mysql表的數據

但是當我嘗試把它不給任何輸出條件(只顯示一個空白表)

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

我無法找到與我的代碼的問題。請提前help.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); 
      } 
     } 
+0

你的數據確實存在嗎? – Drew

+0

您是否嘗試使用調試器逐句通過此代碼?調用填充後,dbdataset表中的行數是多少?如果它是零,那麼你的查詢有問題。嘗試在MySql Workbench中使用相同的值運行它 – Steve

+0

請不要保存這樣的日期數據 – Drew

回答

1

問題可能是,正如@Bjorn-Roger在評論中提到的那樣,'month'和'year'都是SQL中的關鍵字。

我建議你試試:

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

P.S:注意使用[]與字段 '月' 和 '年'。

編輯1

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

+0

@ kavi kavi:這是否工作? – AnkitMittal

相關問題