VC用ADO连接数据库
2009-02-08 15:38
288 查看
ADOCoon.h
// ADOConn.h: interface for the ADOConn class.
//
//////////////////////////////////////////////////////////////////////
#import "c:/Program Files/Common Files/System/ado/msado15.dll" no_namespace rename("EOF","adoEOF") rename("BOF","adoBOF")
#if !defined(AFX_ADOCONN_H__AC448F02_AF26_45E4_9B2D_D7ECB8FFCFB9__INCLUDED_)
#define AFX_ADOCONN_H__AC448F02_AF26_45E4_9B2D_D7ECB8FFCFB9__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class ADOConn
{
// 定义变量
public:
//添加一个指向Connection对象的指针:
_ConnectionPtr m_pConnection;
//添加一个指向Recordset对象的指针:
_RecordsetPtr m_pRecordset;
// 定义方法
public:
ADOConn();
virtual ~ADOConn();
// 初始化—连接数据库
void OnInitADOConn();
// 执行查询
_RecordsetPtr& GetRecordSet(_bstr_t bstrSQL);
// 执行SQL语句,Insert Update _variant_t
BOOL ExecuteSQL(_bstr_t bstrSQL);
void ExitConnect();
};
#endif // !defined(AFX_ADOCONN_H__AC448F02_AF26_45E4_9B2D_D7ECB8FFCFB9__INCLUDED_)
ADOCoon.cpp
// ADOConn.cpp: implementation of the ADOConn class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "SDB.h"
#include "ADOConn.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
// ADOConn.cpp: implementation of the ADOConn class.
//
//////////////////////////////////////////////////////////////////////
ADOConn::ADOConn()
{
}
ADOConn::~ADOConn()
{
}
// 初始化—连接数据库
void ADOConn::OnInitADOConn()
{
// 初始化OLE/COM库环境
::CoInitialize(NULL);
try
{
// 创建Connection对象
m_pConnection.CreateInstance("ADODB.Connection");
// 设置连接字符串,必须是BSTR型或者_bstr_t类型
_bstr_t strConnect="Provider=SQLOLEDB;SERVER=127.0.0.1;Database=SDB;uid=sa;pwd=sa;";
// _bstr_t strConnect="Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=SDB;Data Source=PC25;";
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;
}
// 执行SQL语句,Insert Update _variant_t
BOOL ADOConn::ExecuteSQL(_bstr_t bstrSQL)
{
// _variant_t RecordsAffected;
try
{
// 是否已经连接数据库
if(m_pConnection == NULL)
OnInitADOConn();
// Connection对象的Execute方法:(_bstr_t CommandText,
// VARIANT * RecordsAffected, long Options )
// 其中CommandText是命令字串,通常是SQL命令。
// 参数RecordsAffected是操作完成后所影响的行数,
// 参数Options表示CommandText的类型:adCmdText-文本命令;adCmdTable-表名
// adCmdProc-存储过程;adCmdUnknown-未知
m_pConnection->Execute(bstrSQL,NULL,adCmdText);
return true;
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
return false;
}
}
void ADOConn::ExitConnect()
{
// 关闭记录集和连接
if (m_pRecordset != NULL)
m_pRecordset->Close();
m_pConnection->Close();
// 释放环境
::CoUninitialize();
}
// ADOConn.h: interface for the ADOConn class.
//
//////////////////////////////////////////////////////////////////////
#import "c:/Program Files/Common Files/System/ado/msado15.dll" no_namespace rename("EOF","adoEOF") rename("BOF","adoBOF")
#if !defined(AFX_ADOCONN_H__AC448F02_AF26_45E4_9B2D_D7ECB8FFCFB9__INCLUDED_)
#define AFX_ADOCONN_H__AC448F02_AF26_45E4_9B2D_D7ECB8FFCFB9__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class ADOConn
{
// 定义变量
public:
//添加一个指向Connection对象的指针:
_ConnectionPtr m_pConnection;
//添加一个指向Recordset对象的指针:
_RecordsetPtr m_pRecordset;
// 定义方法
public:
ADOConn();
virtual ~ADOConn();
// 初始化—连接数据库
void OnInitADOConn();
// 执行查询
_RecordsetPtr& GetRecordSet(_bstr_t bstrSQL);
// 执行SQL语句,Insert Update _variant_t
BOOL ExecuteSQL(_bstr_t bstrSQL);
void ExitConnect();
};
#endif // !defined(AFX_ADOCONN_H__AC448F02_AF26_45E4_9B2D_D7ECB8FFCFB9__INCLUDED_)
ADOCoon.cpp
// ADOConn.cpp: implementation of the ADOConn class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "SDB.h"
#include "ADOConn.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
// ADOConn.cpp: implementation of the ADOConn class.
//
//////////////////////////////////////////////////////////////////////
ADOConn::ADOConn()
{
}
ADOConn::~ADOConn()
{
}
// 初始化—连接数据库
void ADOConn::OnInitADOConn()
{
// 初始化OLE/COM库环境
::CoInitialize(NULL);
try
{
// 创建Connection对象
m_pConnection.CreateInstance("ADODB.Connection");
// 设置连接字符串,必须是BSTR型或者_bstr_t类型
_bstr_t strConnect="Provider=SQLOLEDB;SERVER=127.0.0.1;Database=SDB;uid=sa;pwd=sa;";
// _bstr_t strConnect="Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=SDB;Data Source=PC25;";
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;
}
// 执行SQL语句,Insert Update _variant_t
BOOL ADOConn::ExecuteSQL(_bstr_t bstrSQL)
{
// _variant_t RecordsAffected;
try
{
// 是否已经连接数据库
if(m_pConnection == NULL)
OnInitADOConn();
// Connection对象的Execute方法:(_bstr_t CommandText,
// VARIANT * RecordsAffected, long Options )
// 其中CommandText是命令字串,通常是SQL命令。
// 参数RecordsAffected是操作完成后所影响的行数,
// 参数Options表示CommandText的类型:adCmdText-文本命令;adCmdTable-表名
// adCmdProc-存储过程;adCmdUnknown-未知
m_pConnection->Execute(bstrSQL,NULL,adCmdText);
return true;
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
return false;
}
}
void ADOConn::ExitConnect()
{
// 关闭记录集和连接
if (m_pRecordset != NULL)
m_pRecordset->Close();
m_pConnection->Close();
// 释放环境
::CoUninitialize();
}
相关文章推荐
- VC++通过ADO连接数据库(一)
- vc++------利用ADO连接数据库(学习所得)
- VC用ADO连接数据库字符串有感
- VC++ 通过ADO连接数据库查询时返回空值报错的解决方案
- VC++中使用ADO连接数据库
- vc++中ado连接数据库的方法及详细介绍
- VC ado连接数据库
- VC++ADO连接数据库
- 在VC++中利用ADO连接数据库(附带模块下载链接)
- vc++中ADO连接数据库
- VC中用Ado接口代码连接和使用与数据库
- VC++ 6.0 ADO连接数据库中出现的问题
- 20160229 VC++中使用ADO连接数据库
- VC中ADO连接数据库,如何使用Datagrid显示的问题
- vc++ ado连接数据库
- VC++中使用MFC通过ADO连接数据库方法小结
- VC连接ADO数据库
- VC++ 用ADO连接数据库的简单方法
- VC++ ADO连接数据库操作
- VC中ADO连接数据库,自己电脑上可以运行,访问其他数据库则报错