1 用的是ADODB,所以在StdAfx.h文件里面的 #include <afxwin.h> 后面加这句话
2 #import "C:program filescommon filesSystemadomsado15.dll" no_namespace
3 rename("EOF", "adoEOF")
4
5 //CDataManage.h
6 定义变量,数据结构(与数据库中表的字段对应,即保存数据表中字段的信息)及函数(CDataManage()构造函数,~CDataManage(){析构函数,Break()断开数据库连接,ConnectDBase(char* DBPath) //连接数据库,ReadDatabaseInfo(ImageInf imageinfo)//读取数据
7
8 )
9
10
11 //CDataManage.cpp
12 #include "stdafx.h"
13 #include "CDataManage.h"
14
15 extern char g_ImageDBPath[256];//g打头的为全局变量,保存图像路径的变量
16 CDataManage::CDataManage(){ //构造函数
17 m_pConnection=NULL; //初始化连接对象NULL
18 }
19
20 CDataManage::~CDataManage(){//析构函数
21
22 if(m_pConnection!=NULL)
23 {
24 if(m_pConnection->State)
25 m_pConnection->Close();
26 }
27 m_pConnection=NULL;
28 }
29 void CDataManage::Break(){//断开数据库连接
30 if(m_pConnection!=NULL)
31 {
32 if(m_pConnection->State)
33 m_pConnection->Close();
34 }
35 m_pConnection=NULL;
36 }
37
38 bool CDataManage::ConnectDBase(char* DBPath){ //连接数据库
39 // AfxOleInit();
40 HRESULT hr; //创建对象
41 try
42 {
43 hr = m_pConnection.CreateInstance("ADODB.Connection"); //创建Connection对象
44 if (SUCCEEDED(hr)) //创建成功
45 {
46 hr = m_pConnection->Open(DBPath,"","",adConnectUnspecified);
47 //DBPath是字符串类型的数据库存放路径,写时要加双引号
48 //连接数据库注意DataSource路径的写法
49 //AfxMessageBox("连接数据库成功");
50 }
51 m_pRecordset.CreateInstance("ADODB.Recordset");
52 }
53 catch(_com_error e) //联接异常处理
54 {
55 CString errorMsg;
56 errorMsg.Format("连接数据库失败rn错误信息:%s",e.ErrorMessage());
57 // AfxMessageBox(e.Description());
58 return false;//显示错误信息
59 }
60 return TRUE;
61 }
62
63 int CDataManage::ReadDatabaseInfo(ImageInf imageinfo){//读取数据
64 char Sql[100];
65 CString date;
66 CString UBirth;
67 int year,month,day;
68 _variant_t var;
69 sprintf(Sql,"select * from ImageTable where 数据库字字段='%s'",ImageTable.数据库字字段数据);
70 m_pRecordset->Open(Sql,m_pConnection.GetInterfacePtr(), adOpenStatic, adLockOptimistic, adCmdUnknown);
71
72 if(m_pRecordset->GetRecordCount()>0){//数据库中读取到数据
73 m_pRecordset->MoveFirst();
74 while(!m_pRecordset->adoEOF){
75 _variant_t var;
76 var=m_pRecordset->GetCollect("");
77
78
79 if(m_pRecordset->GetState()==1){
80 m_pRecordset->Close();
81 }
82 return 1;
83 }
84
85 if(m_pRecordset->GetRecordCount()>0) {
86 var=m_pRecordset->GetCollect("数据库字段名");
87 g_ImageDBPath=(char)var;//g_ImageDBPath全局变量里面是图片的路径
88 }
89 }
90 //下面几行是循环读取数据库中的记录,根据情况添加
91 /* if(m_pRecordset->GetRecordCount()>0)
92 imageID=10000+m_pRecordset->GetRecordCount()+1;
93 else imageID=10000+1;
94 */
95
96
97 }
98
99
100
View Code
原文链接: https://www.cnblogs.com/cindy-zl24/p/8975392.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/273101
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!