数据库
首页 > 数据库> > BCB中ADO数据库的TADOConnection连接

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;
}

 

标签:AnsiString,BCB,adoCon,ini,TGlobal,tempQuery,ADO,include,TADOConnection
来源: https://blog.csdn.net/weixin_45682858/article/details/121453291