string sql="selecr *from where date"; if (!txtDateB.Text.Trim().Length > 0) { sql += string.Format("date >= '{0}'" , txtDateB.Text.Trim());//选取录入日期大于查询日期的数据 } if (!txtDateE.Text.Trim().Length > 0) { sql += string.Format("and date <= '{0}'", DateTime.Parse(txtDateE.Text.Trim()).ToString("yyyy-MM-dd") + " 23:59:59");//选取录入日期小于查询日期的数据 }
如果SQL语句有了 那不就是差个where条件而已啊
根据你的要求,最终转换成数据库字符串,无外乎下面三条语句 select * from table where convert(varchar(8),Y,112) between '20141110' and '20141120' select * from table where convert(varchar(8),Y,112) >= '20141110' select * from table where convert(varchar(8),Y,112) <= '20141120'
C# 代码里面拼接一下 SQL 就可以了:StringBuilder strSql = new StringBuilder(); strSql.Append("select * from table where 1=1"); if(!string.IsNullOrEmpty(start_date) && !string.IsNullOrEmpty(end_date)) { strSql.AppendFormat(" and y between '{0} 00:00:00' and '{1} 23:59:59.999'",start_date,end_date); } else if(!string.IsNullOrEmpty(start_date)) { strSql.AppendFormat(" and y >='{0} 00:00:00'",start_date); } else{ strSql.AppendFormat(" and y <= '{1} 23:59:59.999'",end_date); }start_date:你输入的开始日期 、end_date: 你输入的结束日期
string sql="selecr *from where date";
if (!txtDateB.Text.Trim().Length > 0)
{
sql += string.Format("date >= '{0}'" , txtDateB.Text.Trim());//选取录入日期大于查询日期的数据
}
if (!txtDateE.Text.Trim().Length > 0)
{
sql += string.Format("and date <= '{0}'", DateTime.Parse(txtDateE.Text.Trim()).ToString("yyyy-MM-dd") + " 23:59:59");//选取录入日期小于查询日期的数据
}
select * from table where convert(varchar(8),Y,112) between '20141110' and '20141120'
select * from table where convert(varchar(8),Y,112) >= '20141110'
select * from table where convert(varchar(8),Y,112) <= '20141120'
strSql.Append("select * from table where 1=1");
if(!string.IsNullOrEmpty(start_date) && !string.IsNullOrEmpty(end_date))
{
strSql.AppendFormat(" and y between '{0} 00:00:00' and '{1} 23:59:59.999'",start_date,end_date);
}
else if(!string.IsNullOrEmpty(start_date))
{
strSql.AppendFormat(" and y >='{0} 00:00:00'",start_date);
}
else{
strSql.AppendFormat(" and y <= '{1} 23:59:59.999'",end_date);
}start_date:你输入的开始日期 、end_date: 你输入的结束日期