C# 查询access数据库中的时间是否在datetimepicker1控件和datetimepicker2控件区间内?
1.access数据库(图1)2.查询窗体(图2)3.查询按钮代码4.错误提示(图3)5.想利用datetimepicker选择一个时间区间,然后查询出区间内登录的用户;...
1.access数据库(图1)2.查询窗体(图2)3.查询按钮代码4.错误提示(图3) 5.想利用datetimepicker选择一个时间区间,然后查询出区间内登录的用户;private void btnSelete_Click(object sender, EventArgs e) { string conditon = ""; DataSet ds; if (cmbbName.Text != "") { conditon += " and userinfo.username='" + cmbbName.Text + "'"; } if (dateTimePickerFrom.ToString() != "" && dateTimePickerTo.ToString() != "") { conditon += " and LastLogin between'" + Convert.ToDateTime(dateTimePickerFrom.Text) + "'and'" + Convert.ToDateTime(dateTimePickerTo.Text) + "'"; } data.dataCon(); strSql = "select UserInfo.ID as 用户ID,UserInfo.UserName as 用户名,UserInfo.UserPwd as 用户密码,UserInfo.LastLogin as 最后登录时间 from UserInfo where 1=1" + conditon; MessageBox.Show(strSql); ds = data.getDataset(strSql); dataGridView.DataSource = ds.Tables[0]; }
展开
展开全部
between的后面,and的前后都少了空格,即
conditon += " and LastLogin between '" +
Convert.ToDateTime(dateTimePickerFrom.Text) + "' and '" +
Convert.ToDateTime(dateTimePickerTo.Text) + "'";
注:印象中是不需要用Convert.ToDateTime进行转换的
另外当你只是一表查询的时候没必要带表名,即
strSql = "select ID as 用户ID,UserName as
用户名,UserPwd as 用户密码,LastLogin as 最后登录时间 from UserInfo
where 1=1 " + conditon;
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询