CSDN博客

img hottomson

ADO执行SQL脚本文件

发表于2004/9/20 14:45:00  1848人阅读


BOOL CADODatabase::Open(LPCTSTR lpstrConnection, LPCTSTR lpstrUserID, LPCTSTR lpstrPassword)
{
 HRESULT hr = S_OK;

 if(IsOpen())
  Close();

 if(strcmp(lpstrConnection, _T("")) != 0)
  m_strConnection = lpstrConnection;

 ASSERT(!m_strConnection.IsEmpty());

 try
 {
  if(m_nConnectionTimeout != 0)
   m_pConnection->PutConnectionTimeout(m_nConnectionTimeout);
  hr = m_pConnection->Open(_bstr_t(m_strConnection), _bstr_t(lpstrUserID), _bstr_t(lpstrPassword), NULL);
  return hr == S_OK;
 }
 catch(_com_error &e)
 {
  dump_com_error(e);
  return FALSE;
 }
 
}
BOOL CADODatabase::ConnectAccess(LPCTSTR lpstrDatabaseName,LPCTSTR lpstrPassword, long lOptions)
{
 CString strConnect;
 strConnect.Format("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=%s; ",lpstrDatabaseName); //修改
 if (lpstrPassword != _T(""))
 {
  strConnect.AppendFormat("Jet OLEDB:Database Password=%s;",lpstrPassword);//修改
 }
 return Open(LPCTSTR(strConnect));
}

BOOL CADODatabase::ConnectSQLServer(LPCTSTR lpstrDatabaseServer, LPCTSTR lpstrDatabaseName, LPCTSTR lpstrUserID, LPCTSTR lpstrPassword)
{
 CString strConnect;
 strConnect.Format("Provider=SQLOLEDB.1; Data Source=%s; Initial Catalog=%s",lpstrDatabaseServer,lpstrDatabaseName);
    return Open(LPCTSTR(strConnect), lpstrUserID, lpstrPassword);
}

BOOL CADODatabase::Execute(LPCTSTR lpstrExec)
{
 ASSERT(m_pConnection != NULL);
 ASSERT(strcmp(lpstrExec, _T("")) != 0);
 _variant_t vRecords;
 
 m_nRecordsAffected = 0;

 try
 {
  m_pConnection->CursorLocation = adUseClient;
  m_pConnection->Execute(_bstr_t(lpstrExec), &vRecords, adExecuteNoRecords);
  m_nRecordsAffected = vRecords.iVal;
  return TRUE;
 }
 catch(_com_error &e)
 {
  dump_com_error(e);
  return FALSE; 
 }
}

BOOL CADODatabase::ExecuteSqlFile(LPCTSTR lpstrFileName)
{
 CStdioFileEx pFile;
 if(!pFile.Open(lpstrFileName,CFile::modeRead | CFile::typeText))
 {
  return FALSE;
 }
 CString strSql,strRead;
 strSql.Empty();
 while(pFile.ReadString(strRead))
 {
  if(strRead.Trim().CompareNoCase(_T("GO"))==0)
  {
   if(!strSql.IsEmpty())
   {
    if(!Execute(strSql))
     return FALSE;
    strSql.Empty();
   }
  }
  else
  {
   strSql+=_T(" ")+strRead;
  }
 }
 return TRUE;
}

0 1

相关博文

我的热门文章

img
取 消
img