VC++中ADO方式訪問資料庫datetime欄位(不帶毫秒時間與帶毫秒時間)
阿新 • • 發佈:2019-01-05
//取得列名
bstrColName = m_pRSet->GetFields()->Item[nCol]->GetName() ;
strColname = (char*)bstrColName ;
//取得當前行當前列值
varCounter.lVal = nCol;
varValue = m_pRSet->GetCollect(varCounter);
CString strTimeGet;
_variant_t varTimeGet;
COleDateTime oleTimeGet;
SYSTEMTIME sysTimeGet;
varTimeGet = varValue;
if (varTimeGet.vt != VT_NULL)
{
//計算毫秒
DATE date = DATE(varValue);
long l = long(date); //天數
date = date - l;
date = date * 24 * 60 * 60;
l = long(date); //秒數
date = date - l;
date = date * 1000;
l = long(date); //毫秒數
oleTimeGet=(COleDateTime)varTimeGet;
VariantTimeToSystemTime(oleTimeGet,&sysTimeGet);
strTimeGet.Format(_T("%d-%d-%d %d:%d:%d.%03d"),sysTimeGet.wYear,sysTimeGet.wMonth,sysTimeGet.wDay,sysTimeGet.wHour,sysTimeGet.wMinute,sysTimeGet.wSecond,l);
strTime = strTimeGet;
}
return TRUE;
}
bstrColName = m_pRSet->GetFields()->Item[nCol]->GetName() ;
strColname = (char*)bstrColName ;
//取得當前行當前列值
varCounter.lVal = nCol;
varValue = m_pRSet->GetCollect(varCounter);
CString strTimeGet;
_variant_t varTimeGet;
COleDateTime oleTimeGet;
SYSTEMTIME sysTimeGet;
varTimeGet = varValue;
if (varTimeGet.vt != VT_NULL)
{
//計算毫秒
DATE date = DATE(varValue);
long l = long(date); //天數
date = date - l;
date = date * 24 * 60 * 60;
l = long(date); //秒數
date = date - l;
date = date * 1000;
l = long(date); //毫秒數
oleTimeGet=(COleDateTime)varTimeGet;
VariantTimeToSystemTime(oleTimeGet,&sysTimeGet);
strTimeGet.Format(_T("%d-%d-%d %d:%d:%d.%03d"),sysTimeGet.wYear,sysTimeGet.wMonth,sysTimeGet.wDay,sysTimeGet.wHour,sysTimeGet.wMinute,sysTimeGet.wSecond,l);
strTime = strTimeGet;
}
return TRUE;
}