Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1696636
  • 博文数量: 263
  • 博客积分: 1218
  • 博客等级: 少尉
  • 技术积分: 2862
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-19 02:33
文章分类

全部博文(263)

文章存档

2020年(12)

2019年(2)

2018年(10)

2016年(1)

2015年(20)

2014年(115)

2013年(46)

2012年(37)

2011年(20)

分类: Windows平台

2013-10-24 20:08:49

其实可以用SQL的Convert()函数或是CAST()函数.
下面是一个例子:


点击(此处)折叠或打开

  1. //按钮的单击事件响应函数
  2. void CAdo_ParameterDlg::OnSelect()
  3. {
  4.     // TODO: Add your control notification handler code here
  5.     _ConnectionPtr pConn;
  6.     _RecordsetPtr pRecordset;
  7.     _CommandPtr pCmd;
  8.     _ParameterPtr Param1;

  9.     CString cError=" 1";    
  10.     try
  11.     {
  12.         pConn.CreateInstance("ADODB.Connection");
  13.         //_bstr_t strConnect="Provier=SQLOLEDB.1;User ID=sa;Password=123456; Server=127.0.0.1\\SQLEXPRESS; Initial Catalog=test;Persist Security Info=true";//SQL Server 的连接字串
  14.         _bstr_t strConnect="Provider=SQLOLEDB.1;Server=127.0.0.1\\SQLEXPRESS;Password=123456;Persist Security Info=true;User ID=sa;Initial Catalog=test";//注127.0.0.1\\SQLEXPRESS改成T75JLZX6ILTGGAL\\SQLEXPRESS的话是肯定可以
  15.         //如果使用127.0.0.1\\SQLEXPRESS要确保sql 2005 Express的"服务和连接的外围应用配置器-->》展开SQLEXPRESS-》下面有个远程连接,选取本地连接和远程连接-》选择同时使用TCP/IP和name pipes,确定

  16.         pConn->Open(strConnect,"","",adModeUnknown);//连接数据库
  17.         cError=" 2";

  18.     }
  19.     catch(_com_error e)
  20.     {
  21.         AfxMessageBox(e.Description()+cError);
  22.         return;
  23.     }
  24.     
  25.     try
  26.     {
  27.         pRecordset.CreateInstance("ADODB.Recordset");
  28.         pCmd.CreateInstance("ADODB.Command");
  29.         pCmd->ActiveConnection=pConn;
  30.                 //代码段3 //这样可以查询时间字段
  31.         pCmd->ActiveConnection=pConn;
  32.         CString sCmd;
  33.         CTime time(1982,2,2,0,0,0);
  34.         cError="3";
  35.         //sCmd.Format("select * from student where birthday= CAST('%s' as datetime)",time.Format("%Y-%m-%d %H:%M:%S") );
  36.         sCmd.Format("select * from student where birthday= CONVERT(varchar(100),'%s',120)",time.Format("%Y-%m-%d %H:%M:%S") );
  37.         
  38.         cError="4";
  39.         pCmd->CommandText=_bstr_t(sCmd);
  40.         
  41.         //end of 代码段3

  42.         //
  43.         pRecordset->Open((IDispatch*)pCmd, vtMissing, adOpenStatic, adLockOptimistic, adCmdUnspecified);
  44.         int i=0;
  45.         while(!pRecordset->adoEOF)
  46.         {
  47.             m_ListCtrl.InsertItem(i,""); //刚开始忘了加入这一行,所以一直没有数据显示
  48.             m_ListCtrl.SetItemText(i,0, (char *) (bstr_t)pRecordset->GetCollect("id"));//m_ListCtrl是一个listControl
  49.             m_ListCtrl.SetItemText(i,1, (char *) (bstr_t)pRecordset->GetCollect("sname"));
  50.             m_ListCtrl.SetItemText(i,2, (char *) (bstr_t)pRecordset->GetCollect("grade"));
  51.             m_ListCtrl.SetItemText(i,3, (char *) (bstr_t)pRecordset->GetCollect("class"));
  52.             m_ListCtrl.SetItemText(i,4, (char *) (bstr_t)pRecordset->GetCollect("birthday"));
  53.             
  54.             i++;
  55.             pRecordset->MoveNext();
  56.         }

  57.     }
  58.     catch(_com_error e)
  59.     {
  60.         AfxMessageBox(e.Description()+cError);
  61.         return;
  62.     }
  63. }
具体的整个实例在这里: 
http://blog.chinaunix.net/uid-25958655-id-3961324.html
阅读(2098) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~