日期:2014-05-16 浏览次数:20492 次
直接上代码:
void CADOConDlg::OnButton1() { // TODO: Add your control notification handler code here HRESULT hr; CString strSql; _ConnectionPtr m_pConnection; _RecordsetPtr m_pRecordset; try { //create the SQL service object hr = m_pConnection.CreateInstance(__uuidof(Connection)); ASSERT( SUCCEEDED(hr) ); strSql.Format("%s","Provider=SQLOLEDB;server=xxx;database=xxx;UID=xxx;PWD=xxx"); m_pConnection->ConnectionString = (_bstr_t)strSql; hr = m_pConnection->Open("","","",adConnectUnspecified); ASSERT( SUCCEEDED(hr) ); //create the record set object for write hr = m_pRecordset.CreateInstance(__uuidof(Recordset)); ASSERT( SUCCEEDED(hr) ); CString strCurrentTime; strSql.Format(_T("select * from 。。。='。。' ")); m_pRecordset->raw_Close(); m_pRecordset->CursorLocation = adUseClient; hr = m_pRecordset->Open((_variant_t)strSql, m_pConnection.GetInterfacePtr(), adOpenKeyset, adLockOptimistic, adCmdText); SYSTEMTIME sys; GetLocalTime( &sys ); strCurrentTime.Format(_T("%d-%d-%d %d:%d:%d"),sys.wYear,sys.wMonth,sys.wDay,sys.wHour,sys.wMinute,sys.wSecond); _variant_t varTime = (LPCTSTR)strCurrentTime; m_pRecordset->Update("datetime",(_variant_t)(COleDateTime)varTime); // m_pRecordset->Update("datetime",(_variant_t)(COleDateTime)sys); //COleDateTime只精确到秒级 //查下MSDN就知道,其实(COleDateTime)强转的类型可以很多 AfxMessageBox("successful"); CString strTimeGet; _variant_t varTimeGet; COleDateTime oleTimeGet; SYSTEMTIME sysTimeGet; varTimeGet=m_pRecordset->GetCollect("datetime"); if (varTimeGet.vt!=VT_NULL) { //strTimeGet=varTimeGet.bstrVal;//此路不通 oleTimeGet=(COleDateTime)varTimeGet; VariantTimeToSystemTime(oleTimeGet,&sysTimeGet); strTimeGet.Format(_T("%d-%d-%d %d:%d:%d"),sysTimeGet.wYear,sysTimeGet.wMonth,sysTimeGet.wDay, sysTimeGet.wHour,sysTimeGet.wMinute,sysTimeGet.wSecond); AfxMessageBox(strTimeGet); } } catch (_com_error e) { //CWnd::MessageBox(e.ErrorMessage(), "error DB select read!", NULL); AfxMessageBox(e.Description()); } }