BCB中ADO数据库的TADOConnection连接

//->->H
#ifndef Unit_GlobalH
#define Unit_GlobalH
//-------------------------------------------------------------------------------------------------------------------------------------------------------------
#endif
#include "vcl.h"
#include 
#include 
#include "inifiles.hpp"

class TGlobal
{
  public:
  TGlobal();
  ~TGlobal();
  public:
  TADOQuery * adoQuery;
  TADOConnection * adoCon;
  void  InitDatabaseConnect () ;
  int   PerformSQL(AnsiString strSQL ,int exMode);
  void  AQEdit(AnsiString strSQL);
} ;
//->->CPP

//-------------------------------------------------------------------------------------------------------------------------------------------------------------

#pragma hdrstop

#include "Unit_Global.h"

//-------------------------------------------------------------------------------------------------------------------------------------------------------------

#pragma package(smart_init)

TGlobal::TGlobal()
{
         adoQuery = new TADOQuery (NULL);
         adoCon   = new TADOConnection(NULL);
         InitDatabaseConnect();
}
TGlobal::~TGlobal()
{
    if(adoQuery)
      delete adoQuery;
    if(adoCon)
      delete adoCon; 
}
void TGlobal::InitDatabaseConnect ()
{
     AnsiString database,user,password,catalog; // ,cathlabid
     TIniFile *ini;
     AnsiString siniFile = ExtractFilePath (Application->ExeName) + "config.ini";
     if(FileExists(siniFile))
     {
         ini = new TIniFile(siniFile);
         database  =ini->ReadString( "setting", "database","");
         user= user=ini->ReadString( "setting", "user","");
         password=ini->ReadString( "setting", "password","");
         catalog=ini->ReadString( "setting", "catalog","");

         adoCon->LoginPrompt=false;
         AnsiString DMSQL =  "Provider=SQLOLEDB.1;Password=""+password+"";Persist Security Info=True;User ID="+user+";Initial Catalog="+catalog+";Use Procedure for Prepare=1;Auto Translate=True;PadoConket Size=4096;Data Source="+database+";";
         adoCon->ConnectionString = DMSQL;
         adoCon->ConnectionTimeout=1000;
         adoCon->Connected = true;
         if(!adoCon->Connected)
         {
           Application->MessageBox("Database:Reason:InitDatabaseConnect","Error",MB_OK+MB_ICONERROR);
           Application->Terminate();
         }

         adoQuery->Connection = adoCon;
         delete ini;
     }

}
int TGlobal::PerformSQL(AnsiString strSQL ,int exMode)
{
  int nReturnValue = ->1;
  TADOQuery * tempQuery = new TADOQuery(NULL);
  tempQuery->Close();
  tempQuery->Connection = adoCon;
  tempQuery->SQL->Text = strSQL;
  if(exMode==0)//查找 select
  {
    tempQuery->Open();
    nReturnValue = tempQuery->RecordCount;
  }
  if(exMode==1)//插入,更新 insert update
  {
     tempQuery->ExecSQL();
  }
  delete tempQuery;
  tempQuery = NULL;
  return nReturnValue;
}

 

上一篇:logrus中输出文件名、行号及函数名


下一篇:简单模拟实现javascript中的call、apply、bind方法