我试图在数据网格视图中获取数据。如果我使用此查询,此代码可以正常工作: - “select * from employee.transaction”
但是当我试图把条件放在它没有给出任何输出时(只显示一张空白表)
我的表有使用mysql server 5.6.25的Int.Im类型的月,年列
我无法找到我的代码问题。请提前帮助.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 :(得分:1)
问题可能是@ Bjorn-Roger在评论中提到的,'月'和'年'都是SQL中的关键词。
我建议你试试:
select * from employee.transaction where department = @department AND [month] = @month AND [year] = @year
P.S:注意[]使用字段'month'和'year'。
修改1
此外,您可能想要检查输入字段“month”和“year”的日期格式是否与数据库表字段的日期格式相同。