博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
再探MFC(八)使用数据库
阅读量:4120 次
发布时间:2019-05-25

本文共 6583 字,大约阅读时间需要 21 分钟。

ADO包装类

A set of ADO classes - version 2.20

 

源文档 <>

 

使用介绍

CADODatabase包装_ConnectionPtr,管理数据库连接.

 

连接数据库例子

//Sample withConnection string for SQL Server

 

CADODatabase* pAdoDb= new CADODatabase();

CStringstrConnection = _T("");

 

strConnection =_T("Provider=MSDASQL;PersistSecurityInfo=False;"

                  "Trusted_Connection=Yes;"

                   "Data Source=Access SqlServer;catalog=sampledb");

pAdoDb->SetConnectionString(strConnection);

 

if(pAdoDb->Open())

  DoSomething();

.

.

.

 

//Sample withConnection String for Access database

 

CADODatabase* pAdoDb= new CADODatabase();

CStringstrConnection = _T("");

 

strConnection =_T("Provider=Microsoft.Jet.OLEDB.4.0;"

              "DataSource=C:\\VCProjects\\ADO\\Test\\dbTest.mdb");

pAdoDb->SetConnectionString(strConnection);

 

if(pAdoDb->Open())

{

  DoSomething();

  .

  .

  .

  pAdoDb->Close();

}

 

delete pAdoDb;

 

注意如果是access 2007版本以上,请使用如下连接字符串

strConnection =_T("Provider=Microsoft.Jet.OLEDB.12.0;")

_T("DataSource=simpledb.accdb;Persist Security Info=False");

CADORecordset包装_RecordsetPtr,管理结果集.

 

 

执行SQL

  1. CADODatabase::Execute执行SQL语句.
  1. 直接用Recordset对象进行查询取得记录集.
  1. 利用command对象来执行SQL命令,command对象在进行存储过程的调用中能真正体现它的作用

 

根据不同的目的执行不同的SQL方法.

 

CADORecordset* pRs =new CADORecordset(m_pDB);

TCHAR sql[256];

_stprintf_s(sql,sizeof(sql), _T("SELECT name,mobilephone FROM contacts"));

if(!pRs->Open(sql))

{

deletepRs;                

}

else

{

while(!pRs->IsEOF())

{

Contactcontact = Contact();

 

pRs->GetFieldValue(_T("name"),contact.name);

pRs->GetFieldValue(_T("mobilephone"),contact.mobilephone);

contacts.push_back(contact);

pRs->MoveNext();

}

pRs->Close();

deletepRs;

pRs= NULL;

}

 

参数

adCmdText:表明CommandText是文本命令

adCmdTable:表明CommandText是一个表名

adCmdProc:表明CommandText是一个存储过程

adCmdUnknown:未知

 

_variant_tCOleDataTime转换

CstringstrValue;

_variant_tvar;

If(var.vt==VT_DATA)

{

        DATA dt = var.data;

        COleDataTime da = COleDateTime(dt);

        strValue = da.Format("%Y-%m-%d%H:%M:%S");

}

 

BOFEOF

BOF、EOF 属性

BOF 指示当前记录位置位于Recordset 对象的第一个记录之前。

EOF 指示当前记录位置位于Recordset 对象的最后一个记录之后。

 

 

常见问题

Broken ADO whencompiling at Windows 7 SP1

I have changed my adoImport.h file to meet specs ofKB2640696.

This solves the problem of broken ADO when compilingat Windows 7 SP1

to make it compatible to run on Windows Vista andWindows XP.

 

 

I include adoImport.h as first include in ado2.h andadox.h

//

//
// adoImport.h
//
// Header file for ado2.h and adox.h
//
// Created by Theo Buys, 27-4-2005
//
// Last revision:
//   $Author: Buys_t $
//     $Date: 4-09-12 12:41 $
// $Revision: 2 $
//
// msado15.dll has namespace ADODB
// msadox.dll has namespace ADOX
// msjro.dll has namespace JRO
//
/////

#if!defined(AFX_ADOIMPORT_H__A8F183D1_116B_4869_9125_16CFF9A03ADA__INCLUDED_)

#defineAFX_ADOIMPORT_H__A8F183D1_116B_4869_9125_16CFF9A03ADA__INCLUDED_
 
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <comdef.h>

#pragma message ( "***adoImport.h:" )

#pragma message ( "***Make sure you have set the library directory for msadox.dll, msado15.dll andmsjro.dll." )
#pragma message ( "***In Visual Studio 2010 go to" )
#pragma message ( "***View > Property Manager > Properties > Microsoft.Cpp.Win32.user >VC++ Directories > Library Directories")
#pragma message ( "***and add $(CommonProgramFiles)\\system\\ado")
 
// CG : In order to use this codeagainst a different version of ADO, the appropriate
// ADO library needs to be used in the #import statement

#import <msadox.dll>

 
/////
// msado15.dll and type libraries (.tlb) issues for building in Windows 7SP1
// see:
//
// 1) Consider the scenario where you are a C++ developer, and you include the
// following line of code in the application:
//
// #import <msado15.dll> rename("EOF","EndOfFile")
//
// 2) Consider the scenario that you recompile an application that must run in
// Windows Vista, in Windows Server 2008, or in later versions of Windows.
// And, you are not using MSJRO. In this scenario, you must change
// #import msado15.dll to the following:
//
// #import <msado60.tlb> rename("EOF","EndOfFile")
//
// 3) Consider the scenario that you are using MSJRO, and you recompile anapplication
// that must run in Windows Vista, in Windows Server 2008, or in a later
// version of Windows.
// 4) Consider the scenario that you recompile your application that must runin Windows XP
// or in Windows Server 2003. In both scenarios, you must change #importmsado15.dll
// to the following:
//
// #import <msado28.tlb> rename("EOF", "EndOfFile")

#import <msado28.tlb> rename("EOF", "EndOfFile")

 
/////
// msjro.dll issues
//
// #import <msjro.dll> no_namespace rename("ReplicaTypeEnum","_ReplicaTypeEnum")
//
// Enable the namespace and suppress warning C4336:
// import cross-referenced type library 'msado28.tlb' before importing'msjro.dll'

#import <msjro.dll> rename("ReplicaTypeEnum", "_ReplicaTypeEnum")

        
#endif //!defined(AFX_ADOIMPORT_H__A8F183D1_116B_4869_9125_16CFF9A03ADA__INCLUDED_)

 

Note that I haveenabled the namespace JRO.

I never put a"using namespace" statement in a header-file but only in asource-file.

 

I hope that this ishelpfull.

 

modified 4-Sep-12 7:46am.

 

源文档 <>

 

 

VS2012 中编译 C 语言项目,如果使用了 scanf 函数,编译时便会提示如下错误:

error C4996: 'scanf': This function or variable may be unsafe. Considerusing scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. Seeonline help for details.

原因是VisualC++ 2012 使用了更加安全的 run-time library routines 。新的Security CRT functions(就是那些带有“_s”后缀的函数),请参见:

下面给出这个问题的解决方案:

方法一:将原来的旧函数替换成新的 Security CRT functions

方法二:用以下方法屏蔽这个警告:

    1. 在预编译头文件stdafx.h里(注意:一定要在没有include任何头文件之前)定义下面的宏:

       #define _CRT_SECURE_NO_DEPRECATE

    2. 或声明 #paramwarning(disable:4996)

    3. 更改预处理定义:

        项目->属性->配置属性->C/C++ -> 预处理器 -> 预处理器定义,增加:

            _CRT_SECURE_NO_DEPRECATE

方法三:方法二没有使用更加安全的 CRT 函数,显然不是一个值得推荐的好方法,但我们又不想一个一个地改函数名,这里还有一个更简便的方法:

在预编译头文件 stdafx.h 里(同样要在没有include任何头文件之前)定义下面的宏:

#define_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1

在链接的时候便会自动将旧函数替换成 Security CRT functions

注意:这个方法虽然使用了新的函数,但是不能消除警告(原因见红字),你还得同时使用方法二(-_-)。即实际应在预编译头文件 stdafx.h 里加入下面两句:

#define _CRT_SECURE_NO_DEPRECATE

#define_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1

 

错误原因解释:

这种微软的警告,主要因为那些C库的函数,很多函数内部是不进行参数检测的(包括越界类的),微软担心使用这些会造成内存异常,所以就改写了同样功能的函数,改写了的函数进行了参数的检测,使用这些新的函数会更安全和便捷。关于这些改写的函数你不用专门去记忆,因为编译器对于每个函数在给出警告时,都会告诉你相应的安全函数,查看警告信息就可以获知,在使用时也再查看一下MSDN详细了解。

参考资料:《

 

源文档 <>

 

 

 

Linkerror: unresolved external symbol ConvertStringToBSTRConvertBSTRToString

解决方案:

添加comsupp.lib release 模式

comsuppw.lib  debug模式

 

#ifdef_DEBUG

#pragmacomment (lib, "comsuppwd.lib")

#else

#pragmacomment (lib, "comsuppw.lib")

#endif

Pastedfrom <>

 

 

参考资料

ADO第一次亲密接触 --ADO开发实践之一

Pastedfrom <>

 

使用ADO实现BLOB数据的存取 -- ADO开发实践之二

Pastedfrom <>

 

ProgrammaticallyInvoking the OLEDB Data Link Config Dialog

oledb数据源连接配置对话框

Pastedfrom <>

 

自己如何正确获取MYSQLADO连接字符串

源文档 <

转载地址:http://mwppi.baihongyu.com/

你可能感兴趣的文章
npm和node升级的正确方式
查看>>
laravel事务
查看>>
springcloud 连续请求 500
查看>>
vue复用新增和编辑表单
查看>>
Ubuntu 16.04 apt-get更换为国内阿里云源
查看>>
laravel部署到宝塔步骤
查看>>
小程序获取access_token
查看>>
navicat远程连接mysql数据库
查看>>
tp5令牌数据无效 解决方法
查看>>
自己的网站与UCenter整合(大致流程)
查看>>
laravel 制作通用的curd 后台操作
查看>>
【小红书2017年笔试】求一个数组中平均数最大的子数组
查看>>
Linux基础系列-定时器与时间管理
查看>>
Linux基础系列-可执行程序的产生过程
查看>>
Linux基础系列-Kernel 初始化宏
查看>>
Linux子系统系列-I2C
查看>>
<iOS>关于自定义description的一点用法
查看>>
Unix 命令,常用到的
查看>>
DLL中建立进程共享数据段需要注意的语法问题
查看>>
服务器端技术----Http请求的处理过程
查看>>