c++ 数据库操作(一)

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX__RECORDSET_H__710C9A64_AF35_487D_9791_C884C0420854__INCLUDED_)

// 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__AF13F190_AA90_4F42_A095_80F2D28FACC7__INCLUDED_)#define AFX_ADOCONN_H__AF13F190_AA90_4F42_A095_80F2D28FACC7__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000class 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_tBOOL ExecuteSQL(_bstr_t bstrSQL);void ExitConnect();};#endif // !defined(AFX_ADOCONN_H__AF13F190_AA90_4F42_A095_80F2D28FACC7__INCLUDED_)

// ADOConn.cpp: implementation of the ADOConn class.
// Download by http://www.codefans.net
//

#include "stdafx.h"
#include "HosptialMan.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.Connection");
		// 设置连接字符串,必须是BSTR型或者_bstr_t类型
		_bstr_t strConnect = "Provider=SQLOLEDB;Server=xx;DataBase=xx;Uid=xx;Pwd=xx";
		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();
}

 

#if !defined(AFX__RECORDSET_H__710C9A64_AF35_487D_9791_C884C0420854__INCLUDED_)
#define AFX__RECORDSET_H__710C9A64_AF35_487D_9791_C884C0420854__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// Machine generated IDispatch wrapper class(es) created by Microsoft Visual C++

// NOTE: Do not modify the contents of this file.  If this class is regenerated by
//  Microsoft Visual C++, your modifications will be overwritten.


// Dispatch interfaces referenced by this interface
class CProperties;
class CFields;

/
// C_Recordset wrapper class

class C_Recordset : public COleDispatchDriver
{
public:
	C_Recordset() {}		// Calls COleDispatchDriver default constructor
	C_Recordset(LPDISPATCH pDispatch) : COleDispatchDriver(pDispatch) {}
	C_Recordset(const C_Recordset& dispatchSrc) : COleDispatchDriver(dispatchSrc) {}

// Attributes
public:

// Operations
public:
	CProperties GetProperties();
	long GetAbsolutePosition();
	void SetAbsolutePosition(long nNewValue);
	void SetRefActiveConnection(LPDISPATCH newValue);
	void SetActiveConnection(const VARIANT& newValue);
	VARIANT GetActiveConnection();
	BOOL GetBof();
	VARIANT GetBookmark();
	void SetBookmark(const VARIANT& newValue);
	long GetCacheSize();
	void SetCacheSize(long nNewValue);
	long GetCursorType();
	void SetCursorType(long nNewValue);
	BOOL GetEof();
	CFields GetFields();
	long GetLockType();
	void SetLockType(long nNewValue);
	long GetMaxRecords();
	void SetMaxRecords(long nNewValue);
	long GetRecordCount();
	void SetRefSource(LPDISPATCH newValue);
	void SetSource(LPCTSTR lpszNewValue);
	VARIANT GetSource();
	void AddNew(const VARIANT& FieldList, const VARIANT& Values);
	void CancelUpdate();
	void Close();
	void Delete(long AffectRecords);
	VARIANT GetRows(long Rows, const VARIANT& Start, const VARIANT& Fields);
	void Move(long NumRecords, const VARIANT& Start);
	void MoveNext();
	void MovePrevious();
	void MoveFirst();
	void MoveLast();
	void Open(const VARIANT& Source, const VARIANT& ActiveConnection, long CursorType, long LockType, long Options);
	void Requery(long Options);
	void Update(const VARIANT& Fields, const VARIANT& Values);
	long GetAbsolutePage();
	void SetAbsolutePage(long nNewValue);
	long GetEditMode();
	VARIANT GetFilter();
	void SetFilter(const VARIANT& newValue);
	long GetPageCount();
	long GetPageSize();
	void SetPageSize(long nNewValue);
	CString GetSort();
	void SetSort(LPCTSTR lpszNewValue);
	long GetStatus();
	long GetState();
	void UpdateBatch(long AffectRecords);
	void CancelBatch(long AffectRecords);
	long GetCursorLocation();
	void SetCursorLocation(long nNewValue);
	C_Recordset NextRecordset(VARIANT* RecordsAffected);
	BOOL Supports(long CursorOptions);
	long GetMarshalOptions();
	void SetMarshalOptions(long nNewValue);
	void Find(LPCTSTR Criteria, long SkipRecords, long SearchDirection, const VARIANT& Start);
	void Cancel();
	LPUNKNOWN GetDataSource();
	void SetRefDataSource(LPUNKNOWN newValue);
	void Save(LPCTSTR FileName, long PersistFormat);
	LPDISPATCH GetActiveCommand();
	void SetStayInSync(BOOL bNewValue);
	BOOL GetStayInSync();
	CString GetString(long StringFormat, long NumRows, LPCTSTR ColumnDelimeter, LPCTSTR RowDelimeter, LPCTSTR NullExpr);
	CString GetDataMember();
	void SetDataMember(LPCTSTR lpszNewValue);
	long CompareBookmarks(const VARIANT& Bookmark1, const VARIANT& Bookmark2);
	C_Recordset Clone(long LockType);
	void Resync(long AffectRecords, long ResyncValues);
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX__RECORDSET_H__710C9A64_AF35_487D_9791_C884C0420854__INCLUDED_)


 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值