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

📄 lrunsql.cpp

📁 LRunSql类是我在做数据库编程方面常借鉴的一个类
💻 CPP
字号:
// LRunSql.cpp: implementation of the LRunSql class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "JXC.h"
#include "LRunSql.h"

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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
_ConnectionPtr LRunSql::m_database;
LRunSql::LRunSql()
{

	m_recordset.CreateInstance(__uuidof(Recordset));
	
}
LRunSql::~LRunSql()
{

}
bool LRunSql::RunSQL(CString sql)
{
	
	_bstr_t sql_=sql;
	try{
	m_recordset=m_database->Execute(sql_,NULL,adCmdText);
	}
	catch(_com_error& e)
	{
		ErrorsPtr pErrors=m_database->GetErrors();
		if (pErrors->GetCount()==0)
		{
			//AfxMessageBox(e.ErrorMessage());
			afxDump<<e.ErrorMessage();
		}
		else
		{
			for (int i=0;i<pErrors->GetCount();i++)
			{
				_bstr_t desc=pErrors->GetItem((long)i)->GetDescription();
			//	AfxMessageBox(desc);
				afxDump<<(char*)desc;
			}
		}
		return false;

	}
	return true;
	
}
bool LRunSql::CheckSQLResult(CString sql)
{
	if(!this->RunSQL(sql)) 
		return false;
	else
	{
		if(!this->m_recordset->adoEOF)
			return true;
		else
			return false;
	}
	return true;
}
void DateToInt(CString date,int& year,int& month,int& day)
{
	int a_1=date.Find('-',0);
	int a_2=date.Find('-',5);

	CString y=date.Mid(0,4);
	CString m=date.Mid(a_1+1,a_2-a_1);
	CString d=date.Mid(a_2+1,date.GetLength()-a_2);

	year=atoi(y);
	month=atoi(m);
	day=atoi(d);
}


bool LRunSql::ConnectDataBase()
{
	try{
	m_database->Open(L"jjj","","",-1);
	}
	catch(...)
	{
		::ShellExecute(NULL,"open","AutoConfig.exe",NULL,NULL,SW_HIDE);
		return false;	
	}  
	return true;
}

bool LRunSql::InitConnectPtr()
{
//	::CoInitializeEx(NULL,COINIT_APARTMENTTHREADED);
	if(FAILED(m_database.CreateInstance(__uuidof(Connection))))
		return false;
	if(ConnectDataBase()==false)
	{
		::Sleep(1000);	
		if(ConnectDataBase()==false)
		{
			MessageBox(NULL,"系统试图自动为您配置ODBC,但由于一些原因没有完成,请手工配置!","系统提示",MB_OK|MB_ICONSTOP);
			return false;
		}
	}
	return true;
}

bool LRunSql::Close()
{
	return (!FAILED(m_database->Close()));

}

bool LRunSql::BeginTrans()
{
return (!FAILED(m_database->BeginTrans()));

}

bool LRunSql::CommitTrans()
{
return (!FAILED(m_database->CommitTrans()));

}
bool LRunSql::RollbackTrans()
{
return (!FAILED(m_database->RollbackTrans()));

}

⌨️ 快捷键说明

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