设计思路很简单,就是把数据封装为DataTable类,封装了类型转换,使用者可以不必考虑任何类型转换,而使用VC提供的CString即可。封装了按行号查找功能,先看代码

#pragma once
#import "c:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF") rename("BOF","adoBOF") #ifndef _SQLSERVERHELPER_H
#define _SQLSERVERHELPER_H
#include<list>
#include "stdafx.h" struct PageInfo
{
int PageCount;
int PageSize;
int PageIndex;
}; class DataTable
{
public:
DataTable();
DataTable(_RecordsetPtr& ptr);
~DataTable(void); void SetPtr(_RecordsetPtr& ptr);
UINT GetColumnCount();
UINT GetRowCount();
BOOL Update();
BOOL GetColumnName(long columnIndex,CString &str);
BOOL GetColumnValue(long columnIndex,CString &str);
BOOL SetColumnValue(long columnIndex,CString &str); BOOL SetValue(CString colName,CString str);
BOOL GetValue(CString colName,CString &str);
BOOL GetValue(long rowIndex,CString colName,CString &str);
BOOL SetValue(long rowIndex,CString colName,CString str); template<class T>
BOOL SetGenValue(CString colName,T val);
template<class T>
BOOL GetGenValue(CString colName,T& val); void MoveToRow(long rowIndex);
BOOL GetColumnValue(long columnIndex,long rowIndex,CString &str);
BOOL SetColumnValue(long columnIndex,long rowIndex,CString &str); _RecordsetPtr& GetSet();
private:
_RecordsetPtr m_pSet;
void NoUserFun();
}; class AdoHelper
{
public:
AdoHelper(void);
~AdoHelper(void); BOOL Close();
void ClearError();
BOOL Open(CString str);
_com_error GetLastError();
BOOL Open(CString str,BOOL openTrans);
BOOL OpenSql(CString sql,DataTable &table);
BOOL OpenTable(CString tableName,DataTable &table);
BOOL GetPagInfo(CString tableName,CString whereStr,PageInfo &info,DataTable &table); FieldsPtr& GetTable();
UINT ExcuteNoQuery(CString sqlStr);
//UINT ExcuteNoQuery(CString sqlStr,void* params,int paramLength);
BOOL Excute(CString sqlStr,DataTable &table);
private:
BOOL useTrans;
_CommandPtr m_pCmd;
_RecordsetPtr m_pSet;
_ConnectionPtr m_pCon;
std::list<_com_error> erorList;
};
#endif

以下是实现代码

#include "stdafx.h"
#include "SqlServerHelper.h" void DataTable::NoUserFun()
{
this->SetGenValue<int>(L"",0);
this->SetGenValue<long>(L"",0l);
int val;
this->GetGenValue<int>(L"",val);
long b;
this->GetGenValue<long>(L"",b);
} void DataTable::MoveToRow(long rowIndex)
{
this->m_pSet->MoveFirst();
for(int i=0;i<rowIndex;i++)
this->m_pSet->MoveNext();
} template<class T>
BOOL DataTable::SetGenValue(CString colName,T val)
{
if(!m_pSet)return FALSE; this->m_pSet->PutCollect(_variant_t(colName),_variant_t(val)); return TRUE;
} template<class T>
BOOL DataTable::GetGenValue(CString colName,T& val)
{
if(!m_pSet)return FALSE; val = (T)(_variant_t)(this->m_pSet->GetCollect(_variant_t(colName))); return TRUE;
} DataTable::DataTable(_RecordsetPtr& ptr)
{
this->m_pSet.CreateInstance(__uuidof(Recordset));
this->m_pSet = ptr;
} _RecordsetPtr& DataTable::GetSet()
{
return this->m_pSet;
} BOOL DataTable::Update()
{
HRESULT hr; hr = this->m_pSet->Update(); return SUCCEEDED(hr);
} void DataTable::SetPtr(_RecordsetPtr& ptr)
{
this->m_pSet = ptr;
} DataTable::DataTable()
{
this->m_pSet.CreateInstance(__uuidof(Recordset));
} DataTable::~DataTable(void)
{
this->m_pSet->Close();
} UINT DataTable::GetRowCount()
{
if(!m_pSet)return 0; return this->m_pSet->GetRecordCount();
} UINT DataTable::GetColumnCount()
{
if(!m_pSet)return 0; return this->m_pSet->Fields->Count;
} BOOL DataTable::GetColumnName(long columnIndex,CString &str)
{
if(!m_pSet)return FALSE; str = (LPCSTR)this->m_pSet->Fields->GetItem(columnIndex)->Name; return TRUE;
} BOOL DataTable::GetColumnValue(long columnIndex,CString &str)
{
if(!m_pSet)return FALSE; str = (LPCSTR)(_bstr_t)this->m_pSet->Fields->GetItem(columnIndex)->Value;
return TRUE;
} BOOL DataTable::SetColumnValue(long columnIndex,CString &str)
{
if(!m_pSet)return FALSE; this->m_pSet->Fields->GetItem(columnIndex)->Value = (_bstr_t)str; return TRUE;
} BOOL DataTable::GetColumnValue(long columnIndex,long rowIndex,CString &str)
{
if(!m_pSet)return FALSE;
if(rowIndex>=this->m_pSet->GetRecordCount())return FALSE; this->MoveToRow(rowIndex); str = (LPCSTR)(_bstr_t)this->m_pSet->Fields->GetItem(columnIndex)->Value; return TRUE;
} BOOL DataTable::SetColumnValue(long columnIndex,long rowIndex,CString &str)
{
if(!m_pSet)return FALSE;
if(rowIndex>=this->m_pSet->GetRecordCount())return FALSE; this->MoveToRow(rowIndex); this->m_pSet->Fields->GetItem(columnIndex)->Value = (_bstr_t)str; return TRUE;
} BOOL DataTable::SetValue(CString colName,CString str)
{
if(!m_pSet)return FALSE; this->m_pSet->PutCollect(_variant_t(colName),_variant_t(str)); return TRUE;
} BOOL DataTable::GetValue(CString colName,CString &str)
{
if(!m_pSet)return FALSE; str = (LPCTSTR)(_bstr_t)(this->m_pSet->GetCollect(_variant_t(colName))); return TRUE;
} BOOL DataTable::GetValue(long rowIndex,CString colName,CString &str)
{
if(!m_pSet)return FALSE; this->MoveToRow(rowIndex); str = (LPCTSTR)(_bstr_t)(this->m_pSet->GetCollect(_variant_t(colName))); return TRUE;
}
BOOL DataTable::SetValue(long rowIndex,CString colName,CString str)
{
if(!m_pSet)return FALSE; this->MoveToRow(rowIndex); this->m_pSet->PutCollect(_variant_t(colName),_variant_t(str)); return TRUE;
} AdoHelper::AdoHelper(void)
{
::CoInitialize(NULL);
HRESULT hr; hr = this->m_pCon.CreateInstance(__uuidof(Connection));
ASSERT(SUCCEEDED(hr)); hr = this->m_pSet.CreateInstance(__uuidof(Recordset));
ASSERT(SUCCEEDED(hr)); hr = this->m_pCmd.CreateInstance(__uuidof(Command));
ASSERT(SUCCEEDED(hr));
} AdoHelper::~AdoHelper(void)
{
try
{
m_pSet->Close();
}
catch(_com_error e)
{
} try
{
m_pCon->Close();
}
catch(_com_error e)
{ }
::CoUninitialize();
} void AdoHelper::ClearError()
{
this->erorList.clear();
} BOOL AdoHelper::Open(CString str)
{
HRESULT hr;
try
{
hr = m_pCon->Open((_bstr_t)str,"","",adConnectUnspecified); return SUCCEEDED(hr);
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
this->erorList.push_back(e);
return FALSE;
}
} BOOL AdoHelper::Open(CString str,BOOL openTrans)
{
BOOL openSucceeded = this->Open(str);
this->useTrans = openTrans; if(!this->m_pCon)return FALSE; long result = this->m_pCon->BeginTrans(); return result==0;
} BOOL AdoHelper::Close()
{
if(useTrans)
{
if(!erorList.empty())
this->m_pCon->RollbackTrans();
else
this->m_pCon->CommitTrans(); }
return TRUE;
} _com_error AdoHelper::GetLastError()
{
_com_error error = this->erorList.front(); this->erorList.pop_front(); return error;
} BOOL AdoHelper::OpenTable(CString tableName,DataTable& table)
{
if(!m_pCon)return FALSE; try
{
HRESULT hr; hr = m_pSet->Open(_variant_t(tableName),(IDispatch*)m_pCon,adOpenKeyset,adLockOptimistic,adCmdTable);
table.SetPtr(m_pSet); return SUCCEEDED(hr);
}
catch(_com_error e)
{
this->erorList.push_back(e);
return FALSE;
}
} BOOL AdoHelper::OpenSql(CString sql,DataTable &table)
{
if(!m_pCon)return FALSE; try
{
HRESULT hr; hr = m_pSet->Open(_variant_t(sql),(IDispatch*)m_pCon,adOpenKeyset,adLockOptimistic,adCmdText);
table.SetPtr(m_pSet); return SUCCEEDED(hr);
}
catch(_com_error e)
{
this->erorList.push_back(e);
return FALSE;
}
} FieldsPtr& AdoHelper::GetTable()
{
ASSERT(m_pSet!=NULL);
return this->m_pSet->Fields;
} UINT AdoHelper::ExcuteNoQuery(CString sqlStr)
{
try
{
this->m_pCmd->ActiveConnection= this->m_pCon;
this->m_pCmd->CommandText=(_bstr_t)sqlStr;
this->m_pCmd->CommandType=adCmdText;
this->m_pCmd->Parameters->Refresh();
variant_t count;
this->m_pCmd->Execute(&count,NULL,adCmdText);
return (int)count;
}
catch(_com_error e)
{
this->erorList.push_back(e);
return 0;
}
} BOOL AdoHelper::Excute(CString sqlStr,DataTable& table)
{
try
{
this->m_pCmd->ActiveConnection= this->m_pCon;
this->m_pCmd->CommandText=(_bstr_t)sqlStr;
this->m_pCmd->CommandType=adCmdText;
this->m_pCmd->Parameters->Refresh();
variant_t count;
table.SetPtr(this->m_pCmd->Execute(&count,NULL,adCmdText));
return (int)count;
}
catch(_com_error e)
{
this->erorList.push_back(e);
return 0;
}
} BOOL AdoHelper::GetPagInfo(CString tableName,CString whereStr,PageInfo &info,DataTable &table)
{
if(!m_pCon)return FALSE; try
{
CString sql;
sql.Format(L"select count(*) c from %s where 1=1 %s;",tableName,whereStr); _RecordsetPtr ptr;
this->m_pCmd->ActiveConnection= this->m_pCon;
this->m_pCmd->CommandText=(_bstr_t)sql;
this->m_pCmd->CommandType=adCmdText;
this->m_pCmd->Parameters->Refresh();
variant_t count;
ptr=this->m_pCmd->Execute(&count,NULL,adCmdText);
int allCount = atoi((_bstr_t)ptr->GetCollect("c")); info.PageCount=(allCount+info.PageSize-1)/info.PageSize;
ptr->Close(); HRESULT hr; sql.Format(L"select top %d * from (select row_number() over (order by [Id]) rownumber,* from %s where 1=1 %s) tt where rownumber>%d;",info.PageSize,tableName,whereStr,(info.PageIndex-1)*(info.PageSize)); hr = m_pSet->Open(_variant_t(sql),(IDispatch*)m_pCon,adOpenStatic,adLockReadOnly,adCmdText);
table.SetPtr(m_pSet); return SUCCEEDED(hr);
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
this->erorList.push_back(e);
return FALSE;
}
}

  

丰富自己的代码库-SqlServerHelper(Ado)的更多相关文章

  1. iOS流行的开源代码库

    本文介绍一些流行的iOS的开源代码库 1.AFNetworking 更新频率高的轻量级的第三方网络库,基于NSURL和NSOperation,支持iOS和OSX.https://github.com/ ...

  2. 打造smali代码库辅助分析

    打造smali代码库辅助分析 在分析Android应用程序的时候,我们往往会插入代码重打包apk来辅助我们分析的工作 一个比较取巧的方法就是先用java写好代码以及相关的调用之后, 然后直接扣出代码 ...

  3. Overview of the Oppia codebase(Oppia代码库总览)

    Oppia is built with Google App Engine. Its backend is written in Python, and its frontend is written ...

  4. 我的github代码库

    我的github代码库地址:https://github.com/gooree.Enjoy coding,enjoy sharing.

  5. 使用GitHub for Windows客户端管理京东代码库项目

    1.下载并安装 GitHub for Windows 客户端 https://windows.github.com/ 2.在京东代码库中新的代码库,可以创建私有的代码库 https://code.jd ...

  6. git代码库误操作还原记录

    先做一些前情提要: 我们项目使用git作为代码管理,同时为了操作更方便,安装了乌龟git(tortoiseGit)工具.以下几乎所有操作都是在乌龟git上进行. 我们的项目是分阶段完成的,在完成上一阶 ...

  7. 15分钟学会使用Git和远程代码库

    git是个了不起但却复杂的源代码管理系统.它能支持复杂的任务,却因此经常被认为太过复杂而不适用于简单的日常工作.让我们诚实一记吧:Git是复杂的,我们不要装作它不是.但我仍然会试图教会你用(我的)基本 ...

  8. linux下搭建svn代码库

    1.安装svn客户端 2.创建svn代码库 1.安装svn客户端 1.1.使用命令安装 1)CentOS $ yum install subversion 2)ubuntu sudo apt-get ...

  9. 针对远程Git代码库使用SSH公匙

    → 运行Git Bash→ 创建SSH公匙和私匙ssh-keygen -t rsa→ 输入SSH公匙存放文件,选择使用默认的,按Enter→ 如果已经存在,提示是否重写,输入n,按Enter→ 打开C ...

随机推荐

  1. 01 Linux入门介绍

    一.Linux 初步介绍 Linux的优点 免费的,开源的 支持多线程,多用户 安全性好 对内存和文件管理优越 系统稳定 消耗资源少 Linux的缺点 操作相对困难 一些专业软件以及游戏支持度不足 L ...

  2. 承接Hololens游戏外包

    近日,微软宣布第三批微软Hololens开发者版开始发货,包括:头显.头显手提包和一个遥控器.前两批开发者版本分别在今年3月30日和5月9日开始发货的. 第三批AR头显Hololens开发者版发货 虽 ...

  3. 无法打开登录所请求的数据库 "xxx"登录失败用户 'NT AUTHORITY\NETWORK SERVICE'

    解决:添加用户,选择NT AUTHORITY\SYSTEM登录名,选择当前数据库的架构. 勾选架构 勾选成员身份.如果不勾选,也会报异常:拒绝了对对象 'FW_ORG' (数据库 'ZW_DWSJ', ...

  4. CE程序

    注:开发工具用VS2008 安装Windows mobile设备中心进行调试. 项目平台 1.窗体属性设置,然后将size改成238, 320 2.效果 3.调试过程选择“部署” 4.配置文件的简单读 ...

  5. thinkphp关闭调试模式(APP_DEBUG => false),导致程序出错

    thinkphp关闭调试模式(APP_DEBUG => false),导致程序出错,开启调试模式,不报错,怎么解决? 查看Logs日志记录: [ --29T09::+: ] 113.108.11 ...

  6. C#程序实现动态调用DLL的研究(转)

    摘 要:在<csdn开发高手>2004年第03期中的<化功大法——将DLL嵌入EXE>一文,介绍了如何把一个动态链接库作为一个资源嵌入到可执行文件,在可执行文件运行时,自动从资 ...

  7. emacs不能使用中文输入法

    参考 http://blog.csdn.net/nomasp/article/details/52138501 根据Fcitx的介绍:当LC_CTYPE为英文时,在Emacs上可能无法使用输入法. : ...

  8. android studio 使用ndk编译.C文件生成so文件

    task buildSo(type: Exec) { //windows commandLine 'ndk-build.cmd', '-C', file('src/main').absolutePat ...

  9. (Array)121. Best Time to Buy and Sell Stock

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  10. [转载]反无人机企业DroneShield利用声音识别侦测无人机

    原文:http://www.cnbeta.com/articles/495071.htm 无人机产业正在蓬勃发展,受益的不仅仅是那些生产小型飞行设备的企业.专家估计仅在澳大利亚就有5万架商用无人机以及 ...