- 15
- 0
- 约5.66万字
- 约 83页
- 2017-08-29 发布于重庆
- 举报
C编程常用模块
1(一):连接SQL数据库
一、连接数据库(SQL):
(一)直接写在程序中
1、导入动态连接库
在StdAfx.h中:
#import C:\Program Files\Common Files\System\ado\msado15.dll no_namespace \
rename(EOF,adoEOF) rename(BOF,adoBOF)
2、定义Provider宏
在StdAfx.h中:
#define Provider Provider=SQLOLEDB.1;Integrated Security=SSPI;\
Persist Security Info=False;Initial Catalog=HRMS
3、初始化
a) .h文件中
private:
HDC m_hDC;
DWORD m_CurrentRecord;
_RecordsetPtr m_pRecordset;
_ConnectionPtr m_pConnection;
b) .cpp文件中
CADOOperation::CADOOperation()
{
try{
m_pConnection.CreateInstance(_uuidof(Connection));
m_pConnection-Open(_bstr_t(Provider), , , adModeUnknown);
}
catch(_com_error e)
{
AfxMessageBox(e.Description(), MB_OK);
exit(0);
}
m_pRecordset.CreateInstance(_uuidof(Recordset));
m_CurrentRecord = -1;
}
4、执行
5、关闭
CADOOperation::~CADOOperation()
{
m_pConnection-Close();
}
(二)通过login.ini文件连接
void CBbbApp::IniAdo()
{
try
{
CString temp;
char filepath[MAX_PATH];
GetModuleFileName(NULL,filepath,MAX_PATH);
temp = theApp.ExtractFilePath(filepath);//获取可执行文件的路径,也就是程序运行的.exe文件所在路径
//运行后,temp的值为程序文件.exe 所在路径
CString strAdoConn;
char temp1[100];
/*********************************************
在同目录下创建login.ini文件,并在其中设置数据库连接字符串,如:
[DatabaseConfig]
Server=. // . 表示本机
Database=BookManage
User=sa
PWD=sa
*********************************************/
GetPrivateProfileString(DatabaseConfig,Server,,temp1,100,temp+login.ini);
strserver = (TCHAR *)temp1;
GetPrivateProfileString(DatabaseConfig,Database,,temp1,100,temp+login.ini);
strdbName = temp1;
GetPrivateProfileString(DatabaseConfig,User,,temp1,100,temp+login.ini);
strUser = temp1;
GetPrivateProfileString(DatabaseC
您可能关注的文档
最近下载
- 《基于西门子s7-1200PLC的变频调速电梯电气控制系统设计》14000字.docx VIP
- 《智能光电系统设计》课件——光学系统设计实例.pptx VIP
- 沪教版初中英语单词表(七年级至九年级全6册).docx VIP
- 吉林省肉牛产业发展的多维剖析与破局之道.docx VIP
- NCCN临床实践指南:软组织肉瘤(2026.v1)PPT课件.pptx VIP
- DB21_T 2973-2018小型水利工程混凝土施工质量管理规程.pdf VIP
- 数字金融对制造业企业绿色技术创新的影响研究.pptx VIP
- 2019-2020中国传媒大学电影学考研真题、考研参考书、复试线、招生人数.pdf VIP
- 大型水库清淤施工组织设计.docx VIP
- 金坑水库清淤工程勘察设计招标公告.DOC VIP
原创力文档

文档评论(0)