TOP

VC++中ADO方式操作数据库之有关“datetime”字段的操作
2014-11-23 17:45:02 】 浏览:5791
Tags:ADO 方式 操作 数据库 有关 datetime 段的

[cpp]
直接上代码:
[cpp]
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());
}
}

VC++中ADO方式操作数据库之有关“datetime”字段的操作 https://www.cppentry.com/bencandy.php?fid=55&id=14405

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇多线程编程示例 下一篇为VC++6.0 添加批量注释和批量取..