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

📄 dbado.~cpp

📁 本源代码实现同时对数据库Oracle和MS SQL的连接
💻 ~CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#include <stdio.h>
#include <objbase.h>
#include <IniFiles.hpp>
#pragma hdrstop
//---------------------------------------------------------------------------
#include "dbado.h"
#include "dbconreg.h"
#include "RWLFile.h"
//---------------------------------------------------------------------------
__fastcall TDB_ADO::TDB_ADO()
{

    try
    {
        CoInitialize(NULL);
        //sp_One = new TADOStoredProc(NULL);
        con    = new TADOConnection(NULL);
    }
    catch(...)
    {
        ;
    }
}

void TDB_ADO::Free()
{
    //TODO: Add your source code here

    //sp_One->Close();
    con->Close();

    CoUninitialize();
}

__fastcall TDB_ADO::~TDB_ADO()
{
    //TODO: Add your source code here
    Free();

    //if(sp_One)  delete sp_One;  sp_One = NULL;
    if(con)     delete con;     con = NULL;
}

bool TDB_ADO::GetConnected()
{
    //TODO: Add your source code here
    return con->Connected;
}

TADOConnection * TDB_ADO::GetConnection()
{
    //TODO: Add your source code here 
    return con;
}
//---------------------------------------------------------------------------
void TDB_ADO::GetLastError(char * pError)
{
    //TODO: Add your source code here
    strcpy(pError, m_strLastError.c_str());
}
//---------------------------------------------------------------------------
bool TDB_ADO::Connect()
{
    //TODO: Add your source code here
    LoadDBOption();



    String strCon;

    switch( m_nDBType )
    {
    case MSSQL:
        strCon = "Provider=SQLOLEDB.1;Persist Security Info=True;User ID=" +
            m_strUser       + ";Password=" +
            m_strPassword   + ";Data Source=" +
            m_strServer     + ";Initial Catalog=" +
            m_strDBName     + ";PLSQLRSet=1";

        break;
    case ORACLE:
        //strCon = "Provider=OraOLEDB.Oracle;Persist Security Info=True;User ID=" +
        strCon = "Provider=MSDAORA.1;Persist Security Info=True;User ID=" +
            m_strUser       + ";Password=" +
            m_strPassword   + ";Data Source=" +
            m_strServer     + ";PLSQLRSet=1";

        break;
    }

    try
    {
        if( GetConnected() )
        {
            return true;//con->Close();
        }
        con->LoginPrompt = false;
        con->ConnectionString = strCon;

        con->Open();


    }
    catch(Exception & e)
    {
        m_strLastError = e.Message;
        char sz[256];
        strcpy(sz, m_strLastError.c_str());
        TRWLFile<char>::WriteItemList("c:\\error.txt", sz, m_strLastError.Length());
    }

    return GetConnected();
}



bool TDB_ADO::LoadDBOption()
{
    //TODO: Add your source code here
    //读取数据库连接信息
    TIniFile  *pIni=NULL;
    try
    {
        char szPath[MAX_PATH];
        memset(szPath, 0, MAX_PATH);
        TConReg::GetDbConRegPath(szPath, MAX_PATH);

        strcat(szPath, "pqm4.ini");

        pIni = new TIniFile(szPath);

        String strDBMS;

        strDBMS         = pIni->ReadString("DataBase","DBMS","ORACLE");
        m_strServer     = pIni->ReadString("DataBase","Server","oracle");
        m_strPassword   = pIni->ReadString("DataBase","Password","12345");
        m_strUser       = pIni->ReadString("DataBase","User","sa");
        m_strDBName     = pIni->ReadString("DataBase","DBName","linbo");

        strDBMS = strDBMS.UpperCase();

        if( strDBMS == "ORACLE" )
        {
            m_nDBType = ORACLE;
        }
        else if( strDBMS == "MSSQL" )
        {
            m_nDBType = MSSQL;
        }
    }
    __finally
    {
        if(pIni)delete pIni; pIni = NULL;
    }

    return true;
}



⌨️ 快捷键说明

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