1、SqlApi介紹html
SqlApi++是一個爲訪問Sql數據庫而編寫的庫。支持對Oracle, SQL Server, DB2, Sybase, Informix, InterBase, SQLBase, MySQL, PostgreSQL, SQLite, ODBC數據庫的訪問。他提供簡單的訪問數據庫接口,開發者能夠根據實際狀況自行對其封裝。他擁有完善的文檔和編程實例,開發者能夠快速入手。sql
2、命令解析數據庫
一、 鏈接數據庫函數Connect,以下:編程
void Connect( const SAString &sDBString , const SAString &sUserID , const SAString &sPassword , SAClient_t eSAClient = SA_Client_NotSpecified, saConnectionHandler_t fHandler = NULL);
參數解析: api
sDBString:要鏈接的數據庫函數 sUserID:登陸用戶名fetch sPassword:登陸密碼spa eSAClient:用於指定鏈接何種數據庫命令行 fHandler:用於指定回調函數code |
2、 綁定sql命令函數setCommandText,以下:
void setCommandText( const SAString &sCmd,SACommandType_t eCmdType = SA_CmdUnknown );
參數解析:
sCmd:sql命令 eCmdType:指定命令類型 |
三、 準備函數Prepare,以下:
virtual void Prepare();
四、執行函數Execute,以下:
virtual void Execute();
注意:若是執行存儲過程當中帶輸入參數,須要先執行 Prepare()函數。
5、 獲取結果函數,以下:
bool FetchNext();
3、使用方法
一、 建立數據庫的鏈接對象;
二、 根據數據庫鏈接對象,建立數據庫命令對象,並綁定要執行的語句或者存儲過程名;
三、 執行語句或者存儲過程;
四、 獲取返回的值。
4、使用舉例
如本機上有一SqlServer數據庫。數據庫名爲LittleBee,登陸用戶名sa,密碼爲tiger。數據庫中有一表叫dbo.DM_Runtime,表結構及數據以下:
![]() |
數據庫中有一存儲過程sp_GetRunDate,存儲過程內容以下:
set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go -- ============================================= -- Author: <Author,,Name> -- Create date: <Create Date,,> -- Description: <Description,,> -- ============================================= CREATE PROCEDURE [dbo].[sp_GetRunDate] AS BEGIN SELECT TOP 1 import_date FROM dbo.DM_Runtime END
使用C++程序調用實例我用的是vs2010以下:
#include "stdafx.h" #include <stdio.h> // for printf #include <SQLAPI.h> // main SQLAPI++ header #pragma comment(lib,"F:\\lib\\SQLAPI3.7.34\\lib\\sqlapi.lib") int _tmain(int argc, _TCHAR* argv[]) { SAConnection con; // connection object SACommand cmd(&con); // command object try { // connect to database (Oracle in our example) con.Connect("127.0.0.1@LittleBee", "sa", "tiger", SA_SQLServer_Client); cmd.setCommandText("sp_GetRunDate"); //cmd.setCommandText("SELECT TOP 1 import_date FROM dbo.DM_Runtime"); // Select from our test table cmd.Execute(); // fetch results row by row and print results while(cmd.FetchNext()) { printf("sp_GetRunDate'%s'\n", cmd.Field("import_date").asString()); } // commit changes on success con.Commit(); printf("Rows selected!\n"); } catch(SAException &x) { // SAConnection::Rollback() // can also throw an exception // (if a network error for example), // we will be ready try { // on error rollback changes con.Rollback(); } catch(SAException &) { } // print error message printf("%s\n", (const char*)x.ErrText()); } return 0; }
若是無誤的話,將會在命令行看到sp_GetRunDate'20130719'結果。
參考文檔以下:
SqlApi的官網:http://www.sqlapi.com/index.html
SqlApi庫下載地址:http://www.sqlapi.com/Download/index.html
固然SqlApi也有破解版,爲了支持正版,這裏就不提供破解版下載連接。親能夠本身在網上收一下。
本實例源碼及文檔:http://yunpan.cn/QD894ZF4gpZpk
須要訪問密碼者,請發我郵箱mokimail126@126.com索取