cplusplus

用C++库连接Oracle, MS SQL, MySQL等数据库

上一篇 / 下一篇  2012-08-11 20:58:29 / 个人分类:转载

最近在网上找到一个好东西SQLAPI++,它是可以访问多个SQL数据库Oracle, SQL Server, DB2, Sybase, Informix, InterBase, SQLBase, MySQL, PostgreSQL)C++库。SQLAPI++直接调用本地目标数据库管理系统(DBMS)的API(不像ADO一样使用OLEDB and/or ODBC 中间层)。SQLAPI++库扮演了一个中间件以间接方便访问数据库的角色,这就是为什么SQLAPI++是访问数据库最快的方法。在开发和发布您的应用程序时不再需要安装和配置OLEDB and/or ODBC的驱动。
SQLAPI支持的开发平台有Microsoft Visual C++,Borland C++ Builder,Gun Project C and C++ Compiler。

示例代码如下:
#include <stdio.h>  // for printf
#include <SQLAPI.h> // main SQLAPI++ header

int main(int argc, char* argv[])
{
    SAConnection con; // 连接数据对象
    SACommand cmd(
        &con,
        "Select fid, fvarchar20 from test_tbl"); // 命令对象,其中包含了一个查询语句,//你在测试的时候可以根据需要修改它。
// 本文转自 C++Builder 研究 - http://www.ccrun.com/article.asp?i=1020&d=ssoqrd
    
    try
    {        
        // 连接数据库
        // 在这个例程中连接的是Oracle数据库,
        // 当然它也可以连接 Sybase, Informix, DB2
        // SQLServer, InterBase, SQLBase and ODBC
        con.Connect("test""tester""tester", SA_Oracle_Client);

        // 执行查询语句
        cmd.Execute();
        // 显示查询后的结果
        while(cmd.FetchNext())
        {
            printf("Row fetched: fid = %ld, fvarchar20 = '%s'\n"
                cmd.Field("fid").asLong(),
                (const char*)cmd.Field("fvarchar20").asString());
        }

        // 提交当前事务
        con.Commit();

        printf("Rows selected!\n");
    }
    catch(SAException &x)
    {
        // 异常处理
        try
        {
            // 退出当前事务
            con.Rollback();
        }
        catch(SAException &)
        {
        }
        // 显示错误信息
        printf("%s\n", (const char*)x.ErrText());
    }
    
    return 0;
}
SQLAPI++的官方网站是www.sqlapi.com,它提供评估版本给客户测试。可惜评估版本的库文件在连接数据库成功后,会弹出一个MessageBox对话框。我在测试它的时候觉得很烦,便把它破解掉了,如果需要可以到我的个人网站去下载它www.szsmart.net,不过只提供BCB的破解版本。

TAG: DataBase database DATABASE

 

评分:0

我来说两句

Open Toolbar