工程下载:
http://www.cppblog.com/Files/dyj057/AdoDbv1.0-2007-1-25.rar
前几日看到有位大哥发布的
ADO
数据库的封装,到处是模板,不免有些头晕,想起自己写的
ADO
数据库封装了,首先声明没有模板。但根据我
3
年使用的经验,确实很实用,
特点:
l
多线程安全的。
l
使用标准
C++
数据类型,屏蔽了
com
数据类型和异常,减少了复杂性。
l
操作简单,直观
1
.下面是使用它进行存储过程的例子代码:
异常的处理请参看:
http://www.cppblog.com/dyj057/archive/2007/01/24/17944.html
#include "AdoConn.h"
#include "AdoStoredProc.h"
#include "comm_exception.h"
using namespace C2217::StdLib;
using namespace C2217::Data;
void main()
{
try
{
CAdoConn _conn("Provider=SQLOLEDB;Data Source=192.9.207.214;Initial Catalog=ibmsweb;User ID=sa; Password=sa;");
_conn.Open();
CAdoStoredProc sp(_conn,"Accounts_Roles_Create");
CAdoStringPara para("Description", "Para9", 50);
sp.AddPara(¶);
int count =0 ;//sp.ExecuteNonQuery();
cout << " affected cout " << count <<endl;
CAdoStringPara *pStringPara = dynamic_cast<CAdoStringPara *>(sp["Description"]);
if(pStringPara)
{
cout << pStringPara->getValue() <<endl;
}
sp.ClearParameters();
//Read to dataset
CAdoRecordSet readResult;
sp.setSpName("Accounts_Users_Select_All");
cout << "Read affected item cout : " << sp.Execute(readResult);
//cout << "Read Count :" << readResult.getRecordCount() <<endl;
}
catch(comm_exception &e)
{
SET_CATCH_POS(e);
std::cout << e;
}
catch(const exception &e)
{
cout << e.what();
}
}
2
.读取数据的例子:
const CAdoConn & _conn =S_DeviceDb::instance()->GetAdoConn();
CString sql;
sql.Format("select * from %s where tag_name = '%s' ",
m_tableName, GetName() );
CAdoRecordSet result;
int count = _conn.Execute(sql, result);
if(count ==1)
{
result.MoveFirst();
SetDescription( result.readStr("description").c_str());
SetUnits( result.readStr("uints").c_str());
SetType( result.readStr("data_type").c_str());
SetWritebale( result.readInt("writeable"));
SetExpress( result.readStr("cacu_express").c_str() );
SetDeviceAddress( result.readInt("device_address"));
SetRegisterAddress( result.readInt("register_address"));
SetDataLen( result.readInt("data_length"));
SetBitsStartIndex( result.readInt("start_bit_index"));
SetBitsCount( result.readInt("bit_count"));
}
3
.删除数据的例子:
const CAdoConn & _conn =S_DeviceDb::instance()->GetAdoConn();
CString sql;
sql.Format("delete from %s where tag_name = '%s'", m_tableName, GetName() );
_conn.ExecuteNonQuery( sql);
4
.更新数据的例子:
const CAdoConn & _conn =S_DeviceDb::instance()->GetAdoConn();
CString sql;
sql.Format("update %s set description='%s', uints='%s', data_type='%s',\
writeable=%d, cacu_express='%s', device_address=%d, register_address=%d, data_length =%d,\
start_bit_index = %d, bit_count = %d where tag_name = '%s' ", m_tableName, GetDescription(), GetUnits(), GetType(),
IsWriteable(), GetExpress(), GetDeviceAddress(), GetRegisterAddress(), GetDataLen(), GetBitsStartIndex(), GetBitsCount(), GetName() );
VERIFY(_conn.ExecuteNonQuery(sql));
posted on 2007-01-26 09:53
天下无双 阅读(1999)
评论(2) 编辑 收藏 引用 所属分类:
C/C++