怎样写这样的SQL语句?

EPS 2004-12-27 09:05:14
COleDateTime dateBegin;
COleDateTime dateEnd;

我想查询这两个时间内的所有数据,请问大家我应该怎样写这个SQL语句?
...全文
128 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
MZP 2004-12-27
  • 打赏
  • 举报
回复
strSQL.Format("Select * From %s Where ((日期 >= '%s' ) AND (日期 <= '%s' ))",........
EPS 2004-12-27
  • 打赏
  • 举报
回复
是呀,这些我都知道,我是这样写的
strSQL.Format("Select * From %s Where ((日期 > %s OR 日期 = %s) AND (日期 < %s OR 日期 = %s))",
(char*)(LPCTSTR)strTableName, (char*)(LPCTSTR)strTimeBegin, (char*)(LPCTSTR)strTimeBegin,
(char*)(LPCTSTR)strTimeEnd, (char*)(LPCTSTR)strTimeEnd);

_variant_t varSQL(strSQL);
try
{
m_pRecordset->Open(varSQL,
theApp.m_pConnection.GetInterfacePtr(), // 获取库接库的IDispatch指针
adOpenDynamic,
adLockOptimistic,
adCmdText);
}
catch (_com_error e)
{
MessageBox("程序出现未知错误,请重新启动程序!", "错误");
return;
}

在运行时出现异常,请大家指教一下.
zhangqu_980371 2004-12-27
  • 打赏
  • 举报
回复
"先把COleDateTime型的数据转成CString再拼sql语句:

SYSTEMTIME systimeBegin;
VariantTimeToSystemTime(dateBegin, &systimeBegin);
CTime tm(systimeBegin);
CString strBegin = tm.Format("%Y-%m-%d %H:%M:%S"); //COleDateTime->CString

SYSTEMTIME systimeEnd;
VariantTimeToSystemTime(dateEnd, &systimeEnd);
CTime tm(systimeEnd);
CString strEnd = tm.Format("%Y-%m-%d %H:%M:%S"); //COleDateTime->CString
"


Kudeet 2004-12-27
  • 打赏
  • 举报
回复
SQL Server数据库的用法中有个小问题,第一个end应该是start

COleDateTime time;
start = time.Format("%Y-%m-%d %H:%M:%S"); ////////////////////end ----> start
COleDateTime time(year,month+1,1,0,0,0);
end = time.Format("%Y-%m-%d %H:%M:%S");
Kudeet 2004-12-27
  • 打赏
  • 举报
回复
以上是SQL Server数据库的用法

如果是ACCESS,是这么用:
COleDateTime start(year,month,1,0,0,0);
COleDateTime end(year,month+1,1,0,0,0);
s = start.Format("%Y-%m-%d %H:%M:%S");
e = end.Format("%Y-%m-%d %H:%M:%S");
sql.Format("select * from OutCord where OutTime>=#%s# and OutTime<=#%s#",s,e);


两种数据库都使用过,能查询符合条件的记录
Kudeet 2004-12-27
  • 打赏
  • 举报
回复
COleDateTime time;
end = time.Format("%Y-%m-%d %H:%M:%S");
COleDateTime time(year,month+1,1,0,0,0);
end = time.Format("%Y-%m-%d %H:%M:%S");
sql.Format("select * from [Transformer] where [DeviceID]='%s' and [CheckTime]>'%s' and [CheckTime]<'%s'",deviceid,start,end);
bohut 2004-12-27
  • 打赏
  • 举报
回复
先把COleDateTime型的数据转成CString再拼sql语句:

SYSTEMTIME systimeBegin;
VariantTimeToSystemTime(dateBegin, &systimeBegin);
CTime tm(systimeBegin);
CString strBegin = tm.Format("%Y-%m-%d %H:%M:%S"); //COleDateTime->CString

SYSTEMTIME systimeEnd;
VariantTimeToSystemTime(dateEnd, &systimeEnd);
CTime tm(systimeEnd);
CString strEnd = tm.Format("%Y-%m-%d %H:%M:%S"); //COleDateTime->CString
beyondtkl 2004-12-27
  • 打赏
  • 举报
回复
什麼數據庫哪??還有裡面保存得字段得類型是??
EPS 2004-12-27
  • 打赏
  • 举报
回复
还是不行呀,还是引发了异常.
用AfxMessageBox(e.ErrorMessage());显示的是IDispatch error#3092
大家帮帮忙呀!!!!!!!!!

4,018

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 数据库
社区管理员
  • 数据库
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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