📄 sqlscriptexecutor.cpp
字号:
// SQLScriptExecutor.cpp: implementation of the CSQLScriptExecutor class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "SQLScriptExecutor.h"
#include "ado.h"
#include "DBAgent.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CSQLScriptExecutor::CSQLScriptExecutor()
{
}
CSQLScriptExecutor::~CSQLScriptExecutor()
{
}
BOOL CSQLScriptExecutor::Execute(CString strScriptFile)
{
CADODatabase* pDB=NULL;
CStringArray array;
CString strSQL ;
CFile fp;
if(!fp.Open(strScriptFile,CFile::modeRead))
return FALSE;
int nCount = fp.GetLength();
char* pBuf = strSQL.GetBuffer(nCount);
fp.Read(pBuf,nCount);
strSQL.ReleaseBuffer();
strSQL.MakeUpper();
int nStartPos = 0;
int nCurPos = -1;
int nEndPos = strSQL.GetLength();
nCurPos=strSQL.Find("GO",nStartPos);
while(nCurPos!=-1)
{
CString tmpStr=_T("");
tmpStr = strSQL.Mid(nStartPos,nCurPos-nStartPos-1);
if(!tmpStr.IsEmpty())
{
array.Add(tmpStr);
}
nStartPos = nCurPos + 2;
nCurPos=strSQL.Find("GO",nStartPos);
}
if(nStartPos<nEndPos)
{
CString tmpStr=_T("");
tmpStr = strSQL.Mid(nStartPos,nEndPos-nStartPos);
array.Add(strSQL);
}
//////////////////////////////////////////////
//Update DB
pDB = CDBAgent::CreateDBSession();
if(pDB == NULL)
{
AfxMessageBox("打开数据库失败!");
return FALSE;
}
long lTrans = pDB->BeginTransaction();
for(int i=0;i<array.GetSize();i++)
{
CString strSQL = array.GetAt(i);
if(strSQL.IsEmpty())
continue;
bool bexec = pDB->Execute(strSQL);
if(!bexec)
{
TRACE("Exec Failure!");
AfxMessageBox("数据库更新错误!");
pDB->RollbackTransaction();
CDBAgent::DestroyDBSession(pDB);
return FALSE;
}
}
pDB->CommitTransaction();
CDBAgent::DestroyDBSession(pDB);
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -