VC++ ADO实现三层架构

ADO底层实现

头文件

// ADOConn.h: interface for the ADOConn class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_ADOCONN_H__B8D01B34_20DA_49AE_9FA1_664D67A28ABD__INCLUDED_)
#define AFX_ADOCONN_H__B8D01B34_20DA_49AE_9FA1_664D67A28ABD__INCLUDED_
#import "c:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF")rename("BOF","adoBOF")

#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语句
    BOOL ExecuteSQL(_bstr_t bstrSQL);
	//断开数据库连接
	void ExitConnect();
};

#endif // !defined(AFX_ADOCONN_H__B8D01B34_20DA_49AE_9FA1_664D67A28ABD__INCLUDED_)


源文件

// ADOConn.cpp: implementation of the ADOConn class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "WordGLXT.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");
		m_pConnection.CreateInstance(__uuidof(Connection));
	   
	   //设置连接字符串
	   _bstr_t strConnect="Provider=SQLOLEDB.1;Integrated Security=SSPI;\
		   Persist Security Info=False;Initial Catalog=WenDGL;Data Source=.\\";
	   //SERVER和UID,PWD的设置根据实际情况来设置
	   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.CreateInstance("ADODB.Recordset");
		//取得表中的记录
        m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
	}
    catch(_com_error e)
	{
		e.Description();
	}
	//返回记录集
	return m_pRecordset;
}

BOOL ADOConn::ExecuteSQL(_bstr_t bstrSQL)
{
	_variant_t RecordsAffected;
	try
	{
		//是否已连接数据库
        if(m_pConnection==NULL)
	     	OnInitADOConn();
/***********************************************************************
*  connection对象的Execute方法说明如下:                               *
*  Execute(_bstr_t CommandText,VARIANT * RecordsAffected,long Options) *
*		其中CommandText是命令字符串,通常是SQL命令                      *
*		参数RecordsAffected是操作完成后所影响的行数                    *
*		参数Options表示CommandText的类型。                             *
*		   adCmdText-文本命令                                          *
*		   adCmdTable-表名                                             *
*		   adCmdProc-存储过程                                          *
*		   adCmdUnknown-未知                                           *
***********************************************************************/
        m_pConnection->Execute(bstrSQL,NULL,adCmdText); //执行SQL语句
		return true;
	}
	catch(_com_error e)
	{
		e.Description();
		return false;
	}
}

void ADOConn::ExitConnect()
{
	//关闭记录集和连接
    if(m_pRecordset!=NULL)
		m_pRecordset->Close();
	m_pConnection->Close();
	//释放环境
	::CoUninitialize();
}


CUsers业务逻辑层

此处以常见的用户登录为例,头文件

// Users.h: interface for the CUsers class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_USERS_H__7AB8B197_FAD4_4C75_B32B_40F799B5A97F__INCLUDED_)
#define AFX_USERS_H__7AB8B197_FAD4_4C75_B32B_40F799B5A97F__INCLUDED_

#include "ADOConn.h"
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class CUsers  
{
private:
	CString Username;
	CString Pwd;
	CString JB;

public:
	CUsers();
	virtual ~CUsers();

	CString GetUsername();
	void SetUsername(CString cUsername);
	CString GetPwd();
	void SetPwd(CString cPwd);
	CString GetJB();
	void SetJB(CString cJB);
	void sql_insert();
	void sql_update(CString cUsername);
	void sql_delete(CString cUsername);
	void sql_updatepwd(CString cUsername);

	int Havename(CString cUsername);
	int HaveCzy(CString cUsername,CString cPwd);
	int HaveCzyjb(CString cUsername,CString cPwd,CString cJB);
};

#endif // !defined(AFX_USERS_H__7AB8B197_FAD4_4C75_B32B_40F799B5A97F__INCLUDED_)


源文件

// Users.cpp: implementation of the CUsers class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "WordGLXT.h"
#include "Users.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CUsers::CUsers()
{

}

CUsers::~CUsers()
{

}

CString CUsers::GetUsername()
{
	return Username;
}

void CUsers::SetUsername(CString cUsername)
{
	Username=cUsername;
}

CString CUsers::GetPwd()
{
	return Pwd;
}

void CUsers::SetPwd(CString cPwd)
{
	Pwd=cPwd;
}

CString CUsers::GetJB()
{
	return JB;
}

void CUsers::SetJB(CString cJB)
{
	JB=cJB;
}

void CUsers::sql_insert()
{
	ADOConn m_AdoConn;
	_bstr_t vSQL;
	vSQL="insert into Users(Username,Pwd,JB)values('"+Username+"','"+Pwd+"','"+JB+"')";
	m_AdoConn.ExecuteSQL(vSQL);
	m_AdoConn.ExitConnect();
}

void CUsers::sql_update(CString cUsername)
{
	ADOConn m_AdoConn;
	_bstr_t vSQL;
	vSQL="update Users set Pwd='"+Pwd+"',JB='"+JB+"' where Username="+" "+cUsername+"'";
	m_AdoConn.ExecuteSQL(vSQL);
	m_AdoConn.ExitConnect();
}

void CUsers::sql_delete(CString cUsername)
{
	ADOConn m_AdoConn;
    m_AdoConn.OnInitADOConn();
	CString sql;
	sql.Format("delete from Users where Username='%s'",cUsername);
	m_AdoConn.ExecuteSQL((_bstr_t)sql);
	m_AdoConn.ExitConnect();
}

int CUsers::Havename(CString cUsername)
{
	ADOConn m_AdoConn;
    m_AdoConn.OnInitADOConn();
	CString sql;
	sql.Format("SELECT*FROM Users WHERE Username='%s'",cUsername);
	_RecordsetPtr m_pRecordset;
	m_pRecordset=m_AdoConn.GetRecordSet((_bstr_t)sql);
	if(m_pRecordset->adoEOF)
		return -1;
	else 
		return 1;
	m_AdoConn.ExitConnect();
}

int CUsers::HaveCzy(CString cUsername,CString cPwd)
{
	ADOConn m_AdoConn;
    m_AdoConn.OnInitADOConn();
	CString sql;
	sql.Format("SELECT*FROM Users WHERE Username='%s' AND Pwd='%s'",cUsername,cPwd);
	_RecordsetPtr m_pRecordset;
	m_pRecordset=m_AdoConn.GetRecordSet((_bstr_t)sql);
	if(m_pRecordset->adoEOF)
		return -1;
	else 
		return 1;
	m_AdoConn.ExitConnect();
}

int CUsers::HaveCzyjb(CString cUsername,CString cPwd,CString cJB)
{
	ADOConn m_AdoConn;
    m_AdoConn.OnInitADOConn();
	CString sql;
	sql.Format("SELECT*FROM Users WHERE Username='%s' AND Pwd='%s' AND JB='%s'",cUsername,cPwd,cJB);
	_RecordsetPtr m_pRecordset;
	m_pRecordset=m_AdoConn.GetRecordSet((_bstr_t)sql);
	if(m_pRecordset->adoEOF)
		return -1;
	else 
		return 1;
	m_AdoConn.ExitConnect();
}	

void CUsers::sql_updatepwd(CString cUsername)
{
	ADOConn m_AdoConn;
	_bstr_t vSQL;
	vSQL="UPDATE Users SET Pwd='"+Pwd+"' WHERE Username="+" '"+cUsername+"'";
	m_AdoConn.ExecuteSQL(vSQL);
	m_AdoConn.ExitConnect();
}

登录按钮


 
void CDialogin::OnBnClickedOk()
{
	//将对话框中编辑框的数据读取到成员变量中
	UpdateData(true);

	//检查数据有效性
	CString strName;
	GetDlgItem(IDC_EDIT_Name)->GetWindowText(strName);
	CString strPwd;
	GetDlgItem(IDC_EDIT_PWD)->GetWindowText(strPwd);
	if (strName == "")
	{
		MessageBox("用户名或密码错误!","文档管理系统");
		return;
	}
				
	//如果读取数据和用户输入不同,则返回
	if(user.HaveCzy(strName,strPwd) != 1)
	{
		MessageBox("用户名或密码错误!","文档管理系统");
		return;
	}

	OnOK();
}

 

你可能感兴趣的:(sql,数据库,delete,insert,vc++,文档管理)