VS2010(MFC)用ADO实现连接SQLserver服务器并实现操作(如增删改查) 您所在的位置:网站首页 mfc连接access数据库增删改查 VS2010(MFC)用ADO实现连接SQLserver服务器并实现操作(如增删改查)

VS2010(MFC)用ADO实现连接SQLserver服务器并实现操作(如增删改查)

2024-06-14 01:08| 来源: 网络整理| 查看: 265

VS2010(MFC)用ADO实现连接SQLserver服务器并实现操作(如增删改查) 一、ado的引用

引用ado的文件,

#import "Debug\\msado15.dll" no_namespace rename("EOF","adoEOF")

当然我是把文件复制到Debug文件下,方便移植,文件其实在这里: c:\Program Files\Common Files\System\ado\msado15.dll,当然没有就去网上下,有就复制过来就ok。名字重命名了一下adoEOF。代码付于最下方。

二、写ADO最底层的类

创建一个类,基于CDatabase,当然也可以不要基类。构造函数里面打开 ::CoInitialize(NULL);析构函数中 ::CoUninitialize();进行释放,同时进行最基础的操作打开数据库Open,插入或更新数据库记录ExecuteSQL,获得SQL语句中的记录集GetRecorSet,(得到数据表中最大序号GetMaxNum。) SQLHelper.h:

#pragma once #include using std::string; class CSQLHelper { public: CSQLHelper(void); ~CSQLHelper(void); //添加一个指向Connection对象的指针: _ConnectionPtr m_pConnection; //添加一个指向Recordset对象的指针: _RecordsetPtr m_pRecordset; public: int Open(string strConnect); void Close(); _RecordsetPtr& GetRecorSet(string sqlStr); int ExecuteSQL(string sqlStr); long GetMaxNum(string sqlStr); private: int isDBConnection; };

SQLHelper.cpp:

#include "StdAfx.h" #include "SQLHelper.h" CSQLHelper::CSQLHelper(void) { //默认数据库连接标记为0 isDBConnection = 0; ::CoInitialize(NULL); } CSQLHelper::~CSQLHelper(void) { //释放OLE ::CoUninitialize(); } //打开数据库连接 int CSQLHelper::Open(string strConnect) { try { // 创建Connection对象 m_pConnection.CreateInstance("ADODB.Connection"); // 设置连接字符串,必须是BSTR型或者_bstr_t类型 // _bstr_t strConnect = "Provider=SQLOLEDB;Server=.;Database=otms;uid=sa;pwd=chc;"; m_pConnection->Open(strConnect.c_str(),"","",adModeUnknown); isDBConnection = 1; return isDBConnection; } catch(_com_error e) { isDBConnection = 0; string str = "打开数据库失败!-------" + e.Description(); return isDBConnection; } } //插入或更新数据库记录 int CSQLHelper::ExecuteSQL(string sqlStr) { string str = ""; if(isDBConnection == 0) { return 0; } try { m_pConnection->Execute(sqlStr.c_str(), NULL, adCmdText); return 1; } catch(_com_error e) { str = "插入或更新数据库失败!-------" + e.Description(); return 0; } } //获得SQL语句中的记录集 _RecordsetPtr& CSQLHelper::GetRecorSet(string sqlStr) { //创建记录集对象 m_pRecordset.CreateInstance(__uuidof(Recordset)); try { m_pRecordset->Open(sqlStr.c_str(), m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText); } catch(_com_error e) { string str = "获取记录集失败!------" + e.Description(); } return m_pRecordset; } //得到数据表中最大序号 long CSQLHelper::GetMaxNum(string sqlStr) { string str = ""; int iMaxNum = 0; if(isDBConnection == 0) { return 0; } try { //创建记录集对象 m_pRecordset.CreateInstance(__uuidof(Recordset)); m_pRecordset->Open(sqlStr.c_str(), m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText); if(!m_pRecordset->adoEOF) { iMaxNum = m_pRecordset->GetCollect("MaxNum"); } return iMaxNum; } catch(_com_error e) { str = "获取最大序号失败!-------" + e.Description(); return 0; } } 三、调用自己写的类

其实这里都可以结束了,所有的类都封装好了,直接调用就好了,举例一个最简单的方式,在(你创建的文件名+Dlg.cpp)文件下添加类头文件(你创建的类),然后直接在OnInitDialog的// TODO: 在此添加额外的初始化代码中添加就ok。 比如你是CSQLHelper类。

CSQLHelper conn; _bstr_t strConnect = L"Provider=SQLOLEDB;Server=1;Database=2;uid=3;pwd=4;";//1为服务器名,2为数据库名,3为用户名,4为密码 conn.Open(strConnect);

就这么简单就连接上了。 增加,删除,修改都很简单。

string sqlStr = (sql语句) ExecuteSQL(sqlStr);

查找就是遍历一下就行(之前在网上找如何遍历找半天找不到)。

_bstr_t vSQL = L"SELECT (sql语句) _RecordsetPtr m_pRecordset; m_pRecordset_=conn.GetRecordSet(vSQL); while(!conn.m_pRecordset->rsEOF) { CString No1 = m_pRecordset_Info->GetCollect("LI_No"); CString DBServerName1 = m_pRecordset_Info->GetCollect("LI_DBServerName"); CString DBName1 = m_pRecordset_Info->GetCollect("LI_DBName"); m_pRecordset2->MoveNext(); } conn2.ExitConnect2();

反正大概就是这个意思我不知道表结构所以随便写了几个。当然新建一个类写操作当然是最好的。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有