资源描述:
《C++编程常用模块》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、1(一):连接SQL数据库一、连接数据库(SQL):(一)直接写在程序中1、导入动态连接库在StdAfx.h中:#import"C:ProgramFilesCommonFilesSystemadomsado15.dll"no_namespacerename("EOF","adoEOF")rename("BOF","adoBOF")2、定义Provider宏在StdAfx.h中:#defineProvider"Provider=SQLOLEDB.1;IntegratedSecurity=SSPI;PersistSec
2、urityInfo=False;InitialCatalog=HRMS"3、初始化a).h文件中private:HDCm_hDC;DWORDm_CurrentRecord;_RecordsetPtrm_pRecordset;_ConnectionPtrm_pConnection;b).cpp文件中CADOOperation::CADOOperation(){try{m_pConnection.CreateInstance(_uuidof(Connection));m_pConnection->Open(_bstr_t(Provi
3、der),"","",adModeUnknown);}catch(_com_errore){AfxMessageBox(e.Description(),MB_OK);exit(0);}m_pRecordset.CreateInstance(_uuidof(Recordset));m_CurrentRecord=-1;}4、执行5、关闭CADOOperation::~CADOOperation(){m_pConnection->Close();}(二)通过login.ini文件连接voidCBbbApp::IniAdo(){try
4、{CStringtemp;charfilepath[MAX_PATH];GetModuleFileName(NULL,filepath,MAX_PATH);temp=theApp.ExtractFilePath(filepath);//获取可执行文件的路径,也就是程序运行的.exe文件所在路径//运行后,temp的值为程序文件.exe所在路径CStringstrAdoConn;chartemp1[100];/*********************************************在同目录下创建login.ini
5、文件,并在其中设置数据库连接字符串,如:[DatabaseConfig]Server=.//.表示本机Database=BookManageUser=saPWD=sa*********************************************/GetPrivateProfileString("DatabaseConfig","Server","",temp1,100,temp+"login.ini");strserver=(TCHAR*)temp1;GetPrivateProfileString("Database
6、Config","Database","",temp1,100,temp+"login.ini");strdbName=temp1;GetPrivateProfileString("DatabaseConfig","User","",temp1,100,temp+"login.ini");strUser=temp1;GetPrivateProfileString("DatabaseConfig","PWD","",temp1,100,temp+"login.ini");strPassword=temp1;strAdoConn.F
7、ormat("driver={SQLServer};SERVER=%s;UID=%s;PWD=%s;DATABASE=%s",strserver,strUser,strPassword,strdbName);m_pCon.CreateInstance(_uuidof(Connection));m_pCon->ConnectionString=(_bstr_t)strAdoConn;m_pCon->Open("","","",NULL);m_pCom.CreateInstance("ADODB.Command");m_pRs.Cr
8、eateInstance(_uuidof(Recordset));m_pRs1.CreateInstance(_uuidof(Recordset));//ADOFLAG=TRUE;}catch(_com_error){//ADOFLAG=FALSE;Flag=F