使用了OLE ADO动态链接库。必须在stadafx.h中加入:
#import "C:/program files/common files/system/ado/msado15.dll"/
no_namespace rename("EOF","adoEOF")
加入了动态库msado15.dll动态库后需要在初始化函数中初始化OLE/COM环境库。
InitInstance()中加入:
if (!AfxOleInit())
{
AfxMessageBox("初始化OLE DLL 失败:(");
return FALSE;
}
OLE DB提供者实现对SQL Sever的标准安全连接串:
CString strConnect;
strConnect=_T("Provider=sqloledb;Data Source=file;"
"Initial Catalog=file;"
"User ID=MyUserName;Password=Mypassword;");
打开记录集:
BOOL CInternetBarApp::OpenRecordSet(_RecordsetPtr &recPtr,CString &strSQL)
{
CInternetBarApp* pApp=(CInternetBarApp*)AfxGetApp();
m_pRecordset.CreateInstance(_uuidof(Recordset));
try
{
recPtr->Open(strSQL.AllocSysString(),m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
}
catch(_com_error e)
{
CString strError;
strError.Format("警告:打开数据库表时发生异常。错误信息:%s",e.ErrorMessage());
AfxMessageBox(strError);
return FALSE;
}
return TRUE;
}
vc获得系统当前时间:
CTime time=CTime::GetCurrentTime();
CString str;
str.Format("Years: %d Months: %d Days: %d, Hours:%d Minutes:%d, Seconds:%d", time.GetYear(), time.GetMonth(), time.GetDay(), time.GetHour(), time.GetMinute(),
time.GetSecond());
vc中获得系统时间转换成sql server表中datetime类型
(1)COleDateTime oletimeVar;
_variant_t var;
给oletimeVar赋值;
var.vt=VT_DATE;
var.date=DATE(oletimeVar);
然后将var作为参数传入到你要添加的语句中
(2)COleDateTime ole_time=COleDateTime::GetCurrentTime();
CString szTime;
szTime=ole_time.Format(VAR_DATEVALUEONLY);
rs_write.m_rs->PutCollect("SellDate",_variant_t(szTime));
rs_write.m_rs->Update();
将一个_variant_t类型的值转化为一个CString
COleVariant var=_variant_t
CString str=var.bstrVal
OR
(LPCTSTR)(_bstr_t)vType
PS:没经验写点东西就是慢,还要花掉大量时间DEBUG。积累经验很重要。
ADO学习
最新推荐文章于 2023-01-08 11:27:19 发布