wcams
wcams
采纳率0%
2016-07-01 02:00 阅读 1.2k

C#渣新求助dataAdapter问题

写了下面这个方法调用了两次,第一次是在FormLoad事件里,成功运行
第二次在按钮事件中未成功,DataGridView没填充数据,也没报错
SQL语句在数据库中运行也没问题

public static void FillMainDgv(string str,DataGridView dgv)
{
try
{
dgv.Rows.Clear();
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(str, DBHelper.connection);
DataSet dataSet = new DataSet("ds");
dataAdapter.Fill(dataSet, "MainTable");
int i = 1;
foreach (DataRow row in dataSet.Tables["MainTable"].Rows)
{
int index = dgv.Rows.Add();
dgv.Rows[index].Cells["Column1"].Value = Convert.ToString(i);
dgv.Rows[index].Cells["id"].Value = row["id"];
dgv.Rows[index].Cells["date"].Value = row["dt"];
dgv.Rows[index].Cells["type"].Value = row["type"];
dgv.Rows[index].Cells["body"].Value = row["body"];
dgv.Rows[index].Cells["money"].Value = row["m"];
dgv.Rows[index].Cells["event1"].Value = row["event"];
dgv.Rows[index].Cells["remark"].Value = row["remark"];
dgv.Rows[index].Cells["time"].Value = row["t"];
i++;
}
dgv.ClearSelection();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
FormLoad的SQL语句
SELECT id, Format([date], 'yyyy/mm/dd') AS dt, type, body, format([money],'0.00') as m, event, remark, Format([time],'yyyy/mm/dd hh:nn:ss') AS t FROM MainTable WHERE DateDiff('d', [date], #2016/06/01#) <=0 AND DateDiff('d', [date], #2016/06/30#) >=0 ORDER BY [date] DESC

Button的SQL语句
SELECT id, Format([date], 'yyyy/mm/dd') AS dt, type, body, format([money],'0.00') as m, event, remark, Format([time],'yyyy/mm/dd hh:nn:ss') AS t FROM MainTable WHERE DateDiff('d', [date], #2016/06/01#) <=0 AND DateDiff('d', [date], #2016/06/30#) >=0 type='支出' AND [event] LIKE '**' OR [remark] LIKE '**' ORDER BY [date] DESC

以上语句在数据库中测试均有结果,请大家帮我看看问题出在什么地方了,谢谢了

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • she6600 岁月落寞忆流年 2016-07-01 04:21

    type='支出' AND [event] LIKE '**' OR [remark] LIKE '**' ORDER BY [date] DESC 前面加上and

    点赞 评论 复制链接分享

相关推荐