标题:[求助]ADO问题急啊
只看楼主
jiangzw625
Rank: 1
等 级:新手上路
帖 子:119
专家分:0
注 册:2006-3-27
 问题点数:0 回复次数:0 
[求助]ADO问题急啊

这是我自己写的封装ADO的类
//cado.h
#include <windows.h>
#import "C:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF","adoEOF")

typedef struct _DatabasePros
{
_bstr_t DBName; //DatabaseName
_bstr_t DBIp; //DB Server Ip
_bstr_t DBPwd; //DB Password
_bstr_t DBUser; //DB user
}DatabasePros;

class CData
{
private:
_ConnectionPtr m_pConn;
_RecordsetPtr * m_pRs;
DatabasePros m_DBp;
public:
CData();
CData(DatabasePros &DBps);
void SetDatabasePros(DatabasePros &DBps);
bool LinkSQLDatabase();
_RecordsetPtr* ReadRecord(_bstr_t SQLName);
bool InsertRecord(_bstr_t SQLName);
bool UpdateRecord(_bstr_t SQLName);
bool DeleteRecord(_bstr_t SQLName);
bool CloseDatabase();
~CData();
};

//cado.cpp
CData::CData(DatabasePros &DBps)
{
try
{
m_pConn.CreateInstance("ADODB.Connection");
m_pRs=new _RecordsetPtr("ADODB.Recordset");
m_DBp=DBps;
}
catch(_com_error e)
{
throw e;
}
}

CData::CData()
{
try
{
m_pConn.CreateInstance("ADODB.Connection");
}
catch(_com_error e)
{
throw e;
}
}

void CData::SetDatabasePros(DatabasePros &DBps)
{
m_DBp=DBps;
}

bool CData::LinkSQLDatabase()
{
try
{
_bstr_t ConnStr="Provider=SQLOLEDB.1;Persist Security Info=True;Initial Catalog=";
ConnStr+=m_DBp.DBName+";Data Source="+m_DBp.DBIp;
m_pConn->Open(ConnStr,m_DBp.DBUser,m_DBp.DBPwd,adModeUnknown);
}
catch(_com_error e)
{
MessageBox(NULL,(LPCTSTR)e.Description(),"操作数据库错误",MB_OK|MB_ICONERROR);
return false;
}
return true;
}

bool CData::InsertRecord(_bstr_t SQLName)
{
VARIANT RsAffected;
try
{
m_pConn->Execute(SQLName,&RsAffected,adCmdText);
}
catch(_com_error e)
{
MessageBox(NULL,(LPCTSTR)e.Description(),"操作数据库错误",MB_OK|MB_ICONERROR);
return false;
}
return true;
}

bool CData::UpdateRecord(_bstr_t SQLName)
{
VARIANT RsAffected;
try
{
m_pConn->Execute(SQLName,&RsAffected,adCmdText);
}
catch(_com_error e)
{
MessageBox(NULL,(LPCTSTR)e.Description(),"操作数据库错误",MB_OK|MB_ICONERROR);
return false;
}
return true;
}

bool CData::DeleteRecord(_bstr_t SQLName)
{
VARIANT RsAffected;
try
{
m_pConn->Execute(SQLName,&RsAffected,adCmdText);
}
catch(_com_error e)
{
MessageBox(NULL,(LPCTSTR)e.Description(),"操作数据库错误",MB_OK|MB_ICONERROR);
return false;
}
return true;
}

_RecordsetPtr* CData::ReadRecord(_bstr_t SQLName)
{
try
{
(*m_pRs)->Open(SQLName,m_pConn.GetInterfacePtr(),adOpenKeyset,adLockOptimistic,adCmdText);
}
catch(_com_error e)
{
MessageBox(NULL,(LPCTSTR)e.Description(),"操作数据库错误",MB_OK|MB_ICONERROR);
throw e;
}
return m_pRs; //调试的时候别的都正常就在这出的问题,返回的时候出出现了一个错误,忽略以后还是正常运行。
}

bool CData::CloseDatabase()
{
m_pConn->Close();
return true;
}

CData::~CData()
{
delete m_pRs;
m_pConn->Release();
}

//main.cpp
#include <iostream>
#include "Cado.h"

int main()
{
::CoInitialize(NULL);
string strName;
_RecordsetPtr* pRs;
DatabasePros ps;
ps.DBIp="127.0.0.1";
ps.DBName="CorLayoutOffice";
ps.DBPwd="aaa";
ps.DBUser="sa";
CData cd(ps);
cd.LinkSQLDatabase();
pRs=cd.ReadRecord("select UnitName from UnitInfo");

if((*pRs)->RecordCount>0)
{
_variant_t var;

for(int i=1;i<=(*pRs)->RecordCount;i++)
{
var=(*pRs)->GetCollect("UnitName");
if(var.vt!=VT_NULL)strName=(LPCSTR)_bstr_t(var);
cout<<strName<<"\n";
(*pRs)->MoveNext();
}

}
cout<<(*pRs)->RecordCount<<endl;
cd.CloseDatabase();
::CoUninitialize();
return 0;
}
??

搜索更多相关主题的帖子: private include system common import 
2006-10-20 20:10



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-97667-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019286 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved