⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 adoconn.cpp

📁 此程序用到了,ADO连接ACCESS数据库,切分窗口,以及不同视图之间进行通信.List Control和Tree Control,用户UI线程等技术.
💻 CPP
字号:
// ADOConn.cpp: implementation of the ADOConn class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
//#include "ADOSQLServer.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.Connection");
		// 设置连接字符串,必须是BSTR型或者_bstr_t类型
		//CString strFilePath;
		CString strConnect;
		//strFilePath = GetCurrentPath();
		//strFilePath += "C:\\ManagePlatform.mdb";
		if (m_strDBPath.IsEmpty())
		{
			CString strFilePath;
			strFilePath = GetCurrentPath();
			strFilePath += "cndnynk.mdb";
		}
		strConnect.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;Password=lnasjm",m_strDBPath);
		//_bstr_t strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\ManagePlatform.mdb";
		m_pConnection->Open((LPSTR)(LPCSTR)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->CursorLocation = adUseClient;
		// 取得表中的记录
		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);
		if (m_pConnection != NULL)
		{
			m_pConnection->Close();
		}
		return true;
	}
	catch(_com_error e)
	{
		AfxMessageBox(e.Description());
		return false;
	}
}

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

CString ADOConn::GetCurrentPath()
{
	//取得当前的工作路径
    CString sPath;
    CString strDBFile;
  //得到当前路径,
    GetModuleFileName(NULL,sPath.GetBufferSetLength (MAX_PATH+1),MAX_PATH);
    sPath.ReleaseBuffer();//剩余的空间被释放掉
    int nPos=sPath.ReverseFind ('\\');//寻找最后一个匹配的"\"
    sPath=sPath.Left(nPos);//取字符串前nPos个字符,变成进程的当前目录,(如c:\dd\ccc变成c:\dd\)

    strDBFile = sPath;
  
	return _T(strDBFile);//传递当前目录
}



void ADOConn::SetDBPath(CString DBPath)
{
	m_strDBPath = DBPath;
}

BOOL ADOConn::OpenDB()
{
		// 初始化OLE/COM库环境 
	::CoInitialize(NULL);
  
	try
	{
		// 创建Connection对象
		m_pConnection.CreateInstance("ADODB.Connection");
		// 设置连接字符串,必须是BSTR型或者_bstr_t类型
		//CString strFilePath;
		CString strConnect;
		if (m_strDBPath.IsEmpty())
		{
			CString strFilePath;
			strFilePath = GetCurrentPath();
			strFilePath += "\\cndnynk.mdb";
			m_strDBPath = strFilePath;
		}
		strConnect.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;User ID=admin;Password=;Jet OLEDB:Database Password=lnasjm",m_strDBPath);
		//_bstr_t strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\ManagePlatform.mdb";
		m_pConnection->Open((LPSTR)(LPCSTR)strConnect,"","",adModeUnknown);
		return TRUE;
	}
	// 捕捉异常
	catch(_com_error e)
	{
		// 显示错误信息
		AfxMessageBox(e.Description());
		return FALSE;
	}

}
BOOL ADOConn::OpenTargerDB()
{
			// 初始化OLE/COM库环境 
	::CoInitialize(NULL);
  
	try
	{
		// 创建Connection对象
		m_pConnection.CreateInstance("ADODB.Connection");
		// 设置连接字符串,必须是BSTR型或者_bstr_t类型
		//CString strFilePath;
		CString strConnect;
		if (m_strDBPath.IsEmpty())
		{
			CString strFilePath;
			strFilePath = GetCurrentPath();
			strFilePath += "\\selected.mdb";
			m_strDBPath = strFilePath;
		}
		strConnect.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;User ID=admin;Password=;Jet OLEDB:Database Password=lnasjm",m_strDBPath);
		//_bstr_t strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\ManagePlatform.mdb";
		m_pConnection->Open((LPSTR)(LPCSTR)strConnect,"","",adModeUnknown);
		return TRUE;
	}
	// 捕捉异常
	catch(_com_error e)
	{
		// 显示错误信息
		AfxMessageBox(e.Description());
		return FALSE;
	}
}
void ADOConn::LogWrite(const char *pLogData)
{
	FILE * p = fopen("c:\\log.txt","a");
	fwrite(pLogData,strlen(pLogData),1,p);
	fwrite("\n",strlen("\n"),1,p);
	fflush(p);
	fclose(p);

}
BOOL ADOConn::DeleteRecord()
{

	try
	{
		if (m_pConnection == NULL)
		{
			OpenTargerDB();
		}
		// Connection对象的Execute方法:(_bstr_t CommandText, 
		// VARIANT * RecordsAffected, long Options ) 
		// 其中CommandText是命令字串,通常是SQL命令。
		// 参数RecordsAffected是操作完成后所影响的行数, 
		// 参数Options表示CommandText的类型:adCmdText-文本命令;adCmdTable-表名
		// adCmdProc-存储过程;adCmdUnknown-未知
		m_pConnection->Execute("delete * from selected",NULL,adCmdText);
		if (m_pConnection != NULL)
		{
			m_pConnection->Close();
		}
		return true;
	}
	catch(_com_error e)
	{
		AfxMessageBox(e.Description());
		return false;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -