主题:求助,有关VC中用ADO技术访问数据库程序中的几个问题
程序:
// ADOConn.cpp: implementation of the ADOConn class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Ado.h"
#include "ADOConn.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
ADOConn::ADOConn()
{
}
ADOConn::~ADOConn()
{
}
void ADOConn::OnInitADOConn()
{
//初始化OLE/COM库环境
::CoInitialize(NULL);
try
{
//创建Connection对象
m_pConnection.CreateInstance("ADODB.Recordset");
//设置连接字符串,必须是BSTR型或者_bstr_t类型
_bstr_t strConnect="Provider=SQL Server; Server=09700E4C1AD34EC; DataBase=Booksback; uid=sa; pwd=123456;";
m_pConnection->Open(strConnect,"","",adModeUnknown);
}
//捕捉异常
catch(_com_error e)
{
//显示错误信息
afxMessageBox(e.Description());
}
}
_RecordsetPtr& ADOConn::GetRecordSet(_bstr_t bstrSQL)
{
try
{
//连接数据库,如果Connection对象为空,则重新连接数据库
if(m_pConnection==NULL)
OnInitADOConn();
//创建记录集对象
m_pRecordset.CreateInstance(_uuidof(Recordset));
//取得表中的记录
m_pRecordset->Open( bstrSQL, m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
}
//捕捉异常
catch(_com_error e)
{
//显示错误信息
afxMessageBox(e.Description());
}
//返回记录集
return m_pRecordset;
}
BOOL ADOConn::ExecuteSQL(_bstr_t bstrSQL)
{
//_variant_t RecordsAffected;
try
{
//是否已经连接数据库
if(m_pConnection==NULL)
OnInitADOConn();
m_pConnection->Execute(bstrSQL,NULL,adCmdText);
return ture;
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
return false;
}
}
void ADOConn::ExitConnect()
{
//关闭记录集和连接
if(m_pRecordset!=NUll)
m_pRecordset->Close();
m_pConnection->Close();
//释放环境
::CoUninitialize();
}
编译程序时,出现下面错误:
msado15.tlh(407) : warning C4146: unary minus operator applied to unsigned type, result still unsigned
(鼠标双击这个错误,指向:
enum RecordCreateOptionsEnum
{
adCreateCollection = 8192,
adCreateStructDoc = -2147483648,
adCreateNonCollection = 0,
adOpenIfExists = 33554432,
adCreateOverwrite = 67108864,
adFailIfNotExists = -1
}; //指针指向第二行: adCreateStructDoc = -2147483648,
这是什么错误啊?怎么改?)
'afxMessageBox' : undeclared identifier (afxMessage是什么东西?)
'ture' : undeclared identifier (上面是ADO技术访问数据库的成员ExecuteSQL的实现,true怎么定义?)
'NUll' : undeclared identifier(NULL为空,怎么定义?)
// ADOConn.cpp: implementation of the ADOConn class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Ado.h"
#include "ADOConn.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
ADOConn::ADOConn()
{
}
ADOConn::~ADOConn()
{
}
void ADOConn::OnInitADOConn()
{
//初始化OLE/COM库环境
::CoInitialize(NULL);
try
{
//创建Connection对象
m_pConnection.CreateInstance("ADODB.Recordset");
//设置连接字符串,必须是BSTR型或者_bstr_t类型
_bstr_t strConnect="Provider=SQL Server; Server=09700E4C1AD34EC; DataBase=Booksback; uid=sa; pwd=123456;";
m_pConnection->Open(strConnect,"","",adModeUnknown);
}
//捕捉异常
catch(_com_error e)
{
//显示错误信息
afxMessageBox(e.Description());
}
}
_RecordsetPtr& ADOConn::GetRecordSet(_bstr_t bstrSQL)
{
try
{
//连接数据库,如果Connection对象为空,则重新连接数据库
if(m_pConnection==NULL)
OnInitADOConn();
//创建记录集对象
m_pRecordset.CreateInstance(_uuidof(Recordset));
//取得表中的记录
m_pRecordset->Open( bstrSQL, m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
}
//捕捉异常
catch(_com_error e)
{
//显示错误信息
afxMessageBox(e.Description());
}
//返回记录集
return m_pRecordset;
}
BOOL ADOConn::ExecuteSQL(_bstr_t bstrSQL)
{
//_variant_t RecordsAffected;
try
{
//是否已经连接数据库
if(m_pConnection==NULL)
OnInitADOConn();
m_pConnection->Execute(bstrSQL,NULL,adCmdText);
return ture;
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
return false;
}
}
void ADOConn::ExitConnect()
{
//关闭记录集和连接
if(m_pRecordset!=NUll)
m_pRecordset->Close();
m_pConnection->Close();
//释放环境
::CoUninitialize();
}
编译程序时,出现下面错误:
msado15.tlh(407) : warning C4146: unary minus operator applied to unsigned type, result still unsigned
(鼠标双击这个错误,指向:
enum RecordCreateOptionsEnum
{
adCreateCollection = 8192,
adCreateStructDoc = -2147483648,
adCreateNonCollection = 0,
adOpenIfExists = 33554432,
adCreateOverwrite = 67108864,
adFailIfNotExists = -1
}; //指针指向第二行: adCreateStructDoc = -2147483648,
这是什么错误啊?怎么改?)
'afxMessageBox' : undeclared identifier (afxMessage是什么东西?)
'ture' : undeclared identifier (上面是ADO技术访问数据库的成员ExecuteSQL的实现,true怎么定义?)
'NUll' : undeclared identifier(NULL为空,怎么定义?)