首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

VC+Sqlserver Sql语句中的日期有关问题

2012-02-17 
VC+Sqlserver Sql语句中的日期问题想用下图中组合成的开始日期和终止日期,最后通过Sql语句选出Sqlserver数

VC+Sqlserver Sql语句中的日期问题
想用下图中组合成的开始日期和终止日期,最后通过Sql语句选出Sqlserver数据表处于两个日期之间的记录
其中两个日期值是通过ComBo Box控件得来的。如下图:


自己所写的语句如下 :
  void CTraceDlg::OnOK() 
{
// TODO: Add extra validation here
//得到选中内容
((CComboBox *)GetDlgItem(IDC_START_DAY))->GetWindowText(m_startDay);

((CComboBox *)GetDlgItem(IDC_START_HOUS))->GetWindowText(m_startHous);

((CComboBox *)GetDlgItem(IDC_START_MINUS))->GetWindowText(m_startMinus);

((CComboBox *)GetDlgItem(IDC_START_SECOND))->GetWindowText(m_startSecond);


((CComboBox *)GetDlgItem(IDC_END_DAY))->GetWindowText(m_endDay);

((CComboBox *)GetDlgItem(IDC_END_HOUS))->GetWindowText(m_endHous);

((CComboBox *)GetDlgItem(IDC_END_MINUS))->GetWindowText(m_endMinus);

((CComboBox *)GetDlgItem(IDC_END_SECOND))->GetWindowText(m_endSecond);

 

  CString sql,time1,time2,id;

  time1.Format("%d-%d-%s %s:%s:%s",2010,12,m_startDay,m_startHous,m_startMinus,m_startSecond);
time2.Format("%d-%d-%s %s:%s:%s",2011,12,m_endDay,m_endHous,m_endMinus,m_endSecond);
sql.Format("select * from Demo where etime between %s and %s",time1,time2);
  m_pRs=m_AdoConn.GetRecordSet(_bstr_t(sql));
 
::PostMessage(((CMainFrame*)AfxGetApp()->m_pMainWnd)->m_DemoView->GetSafeHwnd(),WM_TRACEREVIRE,NULL,NULL);
  

  m_AdoConn.ExitConn();
   
CDialog::OnOK();
}

每次编译成功后运行。提示‘06’(就是小时数)附近有语法错误,然后就停止运行了。想了好久,可能是把控件得到在值组合成日期数据时出错了,或Sql语句写的有问题,还是不知道要怎么改,还望高手们指教 。

[解决办法]
sql.Format("select * from Demo where etime between %s and %s",time1,time2);
//-->

sql.Format("select * from Demo where etime between '%s' and '%s'",time1,time2);

热点排行