关于查询指定时间段数据的问题

tyfyang 2012-01-01 01:31:06
DateTime MyDate1 = Convert.ToDateTime(TextBox2.Text.ToString());
DateTime MyDate2 = Convert.ToDateTime(TextBox3.Text.ToString());
string gys = "select * from spmx where sj between'" +MyDate1 + "'and '" +MyDate2 + "'";
OleDbCommand cmd1 = new OleDbCommand(gys, con);
GridView1.DataSource = cmd1.ExecuteReader();
GridView1.DataBind();
这段代码是否有问题,请指教
...全文
48 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
暖枫无敌 2012-01-01
  • 打赏
  • 举报
回复
1、首先你2个文本框中输入的日期要是合法的日期,否则转型会失败。

2、查询语句间要有空格
string gys = "select * from spmx where sj between '" +MyDate1 + "' and '" +MyDate2 + "'";
3、建议使用DataSet


DateTime MyDate1 = Convert.ToDateTime(TextBox2.Text.ToString());
DateTime MyDate2 = Convert.ToDateTime(TextBox3.Text.ToString());
string gys = "select * from spmx where sj between '" +MyDate1 + "' and '" +MyDate2 + "'";
OleDbDataAdapter adapter = new OleDbDataAdapter(gys, con);
DataSet ds = new DataSet();
adapter.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
DENQH 2012-01-01
  • 打赏
  • 举报
回复

GridView1.DataSource = cmd1.ExecuteReader();
好象不能直接绑定sqlReader

用SqldatAapar.Fill()吧

110,502

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧