基于 OLEDB 开发

安装: 获取unvdb_oledb_windows_x64.zip安装包,解压放到 Windows 客户机的某个目录下加压,比如解压到C:\unvdb_oledb_windows_x64目录。然后用 Administrator 用户运行cmd.exe命令行,并进入到 unvdboledb.dll 所在的目录,执行regsvr32 unvdbolede.dll命令注册驱动服务,注册成功时会弹出提示框,提示内容为:DllRegisterServer 在 unvdbolede.dll 已经成功。

../../_images/unvdb_oledb_windows_x64.png

至此就安装完成。

开源链接:https://github.com/yyjdelete/PgOleDb

编译方法:用Visual studio 2022 打开工程,直接编译即可生成。

UDB-TX OLEDB 编程示例(更多编程方法,请参考微软官方文档 https://learn.microsoft.com/en-us/sql/ado/guide/ado-programmer-s-guide?view=sql-server-ver16):

UDB-TX OLEDB连接参数为:”Provider=PostgreSQL OLE DB Provider;Data Source=192.168.2.151;location=unvdb;User ID=unvdb;password=123456;”

其中:

  • Provider 为驱动类型,固定为=PostgreSQL OLE DB Provider

  • Data Source 为数据库所在主机的IP地址

  • location 为数据库名称

  • UserID 为数据库用户名称

  • password 为数据库用户的访问密码

UDB-TX OLEDB兼容微软OLEDB标准,除了连接参数不同外,其他接口完全相同。

下面的例子演示了通过OLEDB驱动访问UDB-TX数据库的系统表pg_tables。

#include<iostream> 
#include"vector" 
#import"C:\Program Files\Common Files\System\ado\msado15.dll" no namespace rename("EOF","adoEOF") 
using namespace std; 

int main(int argc, char* argv[]) 
{ 
    CoInitialize(NULL); 

    _ConnectionPtr pConn=NULL; 
    _RecordsetPtr pRec=NULL; 
    _CommandPtr pCmd=NULL; 
    pConn.CreateInstance(__uuidof(Connection)); 
    pRec.CreateInstance(__uuidof(Recordset)); 
    pCmd.CreateInstance(__uuidof(Command)); 

    try 
    { 
        _bstr_t strConnect  =  "Provider  =  PostgreSQLOLEDBProvider;Data Source  =  192.168.2.151;location  =  unvdb;UserID  =  unvdb;password  =  123456;"; 
        pConn->Open(strConnect,"","",adModeUnknown); 
    } 
    catch(_com_error& e) 
    { 
        cout  <<  "Initial failed!"  <<  endl; 
        cout  <<  e.Description()  <<  endl; 
        cout  <<  e.HelpFile()  <<  endl; 
        return 0; 
    } 

    try 
    {
    pRec = pConn->Execute("select * from pg_tables", NULL, adCmdText);
    if (!pRec->BOF)
    {
        pRec->MoveFirst();
    }
    else
    {
        cout << "Data is empty!" << endl;
        return 0;
    }

    vector<_bstr_t> column_name;
    for (int i = 0; i < pRec->Fields->GetCount(); i++)
    {
        cout << pRec->Fields->GetItem(_variant_t((long)i))->Name << " ";
        column_name.push_back(pRec->Fields->GetItem(_variant_t((long)i))->Name);
        cout << endl;
    }

    while (!pRec->adoEOF)
    {
        for (vector<_bstr_t>::iterator Itr = column_name.begin(); Itr != column_name.end(); Itr++)
        {
            if (pRec->GetCollect(*Itr).vt != VT_NULL)
            {
                cout << (_bstr_t)pRec->GetCollect(*Itr) << " ";
            }
            else
            {
                cout << "NULL" << endl;
            }
        }
        pRec->MoveNext();
        cout << endl;
    }
    catch (_com_error& e)
    {
        cout << e.Description() << endl;
        cout << e.HelpFile() << endl;
        return 0;
    }

    try
    {
        pRec->Close();
        pConn->Close();
    }
    catch (_com_error& e)
    {
        cout << e.Description() << endl;
        cout << e.HelpFile() << endl;
        return 0;
    }

    CoUninitialize();
    return 0;
    }
}