前台有两个TextBox控件,一个ID为datestart,一个ID为dateend,分别标识开始和结束的日期。用来从新闻表中查询发表日期在datestart和dateend之间的文章。
现在我在后台获取这两个TextBox的输入值,
string datestart = this.datestart.text.tostring().trim();
string dateend = this.dateend.text.tostring().trim();
定义sql语句;
string whereStr="select * from Xk_Content";
if (datestart.Length > 0)
{
string startstr = datestart.ToString().Trim();
DateTime dt1 = DateTime.ParseExact(datestart, "yyyy-MM-dd HH:mm:ss", null);
whereStr += " and AddDate > '" + dt1 + "' ";
}
if (dateend.Length > 0)
{
string endstr = dateend.ToString().Trim();
DateTime dt2 = DateTime.ParseExact(dateend, "yyyy-MM-dd HH:mm:ss", null);
whereStr += " and AddDate < '"+dt2+"' ";
}
然后执行查询!
为什么会提示:该字符串未被识别为有效的 DateTime。
现在我在后台获取这两个TextBox的输入值,
string datestart = this.datestart.text.tostring().trim();
string dateend = this.dateend.text.tostring().trim();
定义sql语句;
string whereStr="select * from Xk_Content";
if (datestart.Length > 0)
{
string startstr = datestart.ToString().Trim();
DateTime dt1 = DateTime.ParseExact(datestart, "yyyy-MM-dd HH:mm:ss", null);
whereStr += " and AddDate > '" + dt1 + "' ";
}
if (dateend.Length > 0)
{
string endstr = dateend.ToString().Trim();
DateTime dt2 = DateTime.ParseExact(dateend, "yyyy-MM-dd HH:mm:ss", null);
whereStr += " and AddDate < '"+dt2+"' ";
}
然后执行查询!
为什么会提示:该字符串未被识别为有效的 DateTime。
你那个冒号用了中文的冒号,改为英文冒号试试看?
or
DateTime.ParseExact(dateend, "yyyy-MM-dd HH:mm:ss", null); 单步调试一下.datestart和dateend的值,仔细看下
其次,whereStr += " and AddDate < '"+dt1+"' " 和whereStr += " and AddDate < '"+dt2+"' "
这两句,在时间类型后面加上ToString()
全角冒号吗?
要标识时间日期用#。
该字符串未被识别为有效的 DateTime。
if (datestart.Length > 0)
行 22: {
行 23: DateTime dt1 = DateTime.ParseExact(datestart, "yyyy-MM-dd HH:mm:ss", null);
的数据类型是smalldatetime
和这个有关系吗?
DateTime d = Convert.ToDateTime(a);
可以在sql查询的时候转换一下datetime@endtime=convert(varchar(10),endtime,120)