Window下VS运行达梦DPI
環境準備
程序員開發手冊內容如下:
dpi程序在編譯的過程中需要用到的DM頭文件、鏈接文件、動態庫文件都在達夢安裝目錄的drivers\dpi\下,include中是頭文件。如下:
使用64位的DPI接口時,需要添加DM64 的宏,即 Linux平臺下編譯程序添加“ DDM64 ”編譯參數,Windows平臺下則在相應工程的預處理器定義中添加DM64 。
在VS上新建C的項目
創建c++項目工程,添加源文件。
進行項目配置
頭文件配置
1. 添加頭文件路徑
放到項目名稱上,右鍵點擊屬性
左側點擊C/C++
2. 引入所需的頭文件
右鍵點擊頭文件–》添加–》現有項–將include下面的h文件引入到項目中
鏈接文件配置
1. 添加鏈接文件
放到項目名稱上,右鍵點擊屬性
2. 項目引入鏈接文件
資源文件右鍵添加現有項,添加dmdpi.lib。
添加動態庫文件
右鍵項目==》生成解決方案==》將動態庫文件都復制到生成的項目目錄下:
在日志中可以看到生成的目錄
添加編譯參數DM64
放到項目名稱上,右鍵點擊屬性
運行項目
新建C文件
名字為dm_crud_cpp文件
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "DPI.h" #include "DPIext.h" #include "DPItypes.h" #define DM_SVR "127.0.0.1:5236" #define DM_USER "SYSDBA" #define DM_PWD "SYSDBA" #define ROWS 1 dhenv henv; /* 環境句柄 */ dhcon hcon; /* 連接句柄 */ dhstmt hstmt; /* 語句句柄 */ dhdesc hdesc; /* 描述符句柄 */ DPIRETURN rt; /* 函數返回值 */ #define DPIRETURN_CHECK(rt, hndl_type, hndl) if(!DSQL_SUCCEEDED(rt)){dpi_err_msg_print(hndl_type,hndl); return rt;} #define FUN_CH ECK(rt) if(!DS QL_SUCCEEDED(rt)){got o END;} /****************************************************** Notes: 獲取錯誤信息 *******************************************************/ void dpi_err_msg_print(sdint2 hndl_type, dhandle hndl) {sdint4 err_code;sdint2 msg_len;sdbyte err_msg[SDBYTE_MAX];/* 獲取錯誤信息集合 */dpi_get_diag_rec(hndl_type, hndl, 1, &err_code, err_msg, sizeof(err_msg), &msg_len);printf("err_msg = %s, err_code = %d\n", err_msg, err_code); }/* 創建新連接*/ DPIRETURN dm_dpi_connect(sdbyte* server, sdbyte* uid, sdbyte* pwd) {/* 申請環境句柄 */rt = dpi_alloc_env(&henv);DPIRETURN_CHECK(rt, DSQL_HANDLE_ENV, henv);/* 申請連接句柄 */rt = dpi_alloc_con(henv, &hcon);DPIRETURN_CHECK(rt, DSQL_HANDLE_DBC, hcon);/* 連接數據庫服務器 */rt = dpi_login(hcon, server, uid, pwd);DPIRETURN_CHECK(rt, DSQL_HANDLE_DBC, hcon);if (!DSQL_SUCCEEDED(rt)){dpi_err_msg_print(DSQL_HANDLE_DBC, hcon);return rt;}return DSQL_SUCCESS; }/* 初始化環境*/ DPIRETURN dm_init_env(){/* 分配語句句柄 */rt = dpi_alloc_stmt(hcon, &hstmt);//清空表,初始化測試環境DPIRETURN_CHECK(rt, DSQL_HANDLE_DBC, hcon);dpi_exec_direct(hstmt, (sdbyte*)"drop table dpi_demo");rt = dpi_exec_direct(hstmt, (sdbyte*)"create table dpi_demo(c1 int, c2 char(20), c3 varchar(50), c4 numeric(7,3), c5 timestamp(5), c6 clob, c7 blob)");DPIRETURN_CHECK(rt, DSQL_HANDLE_STMT, hstmt);rt = dpi_free_stmt(hstmt);DPIRETURN_CHECK(rt, DSQL_HANDLE_STMT, hstmt);printf("dm init table success\r\n");}/* insert數據*/ DPIRETURN dm_insert_data(sdint4* c1) {//插入數據//sdint4 c1 = 0; /* 與字段匹配的變量 */sdbyte c2[10];sdbyte c3[10];ddouble c4;dpi_timestamp_t c5;sdbyte c6[18];sdbyte c7[18];slength c1_ind_ptr;slength c2_ind_ptr; /* 緩沖區長度 */slength c3_ind_ptr;slength c4_ind_ptr = 0;slength c5_ind_ptr = 0;slength c6_ind_ptr;slength c7_ind_ptr;/* 分配語句句柄 */rt = dpi_alloc_stmt(hcon, &hstmt);DPIRETURN_CHECK(rt, DSQL_HANDLE_DBC, hcon);/* 準備 sql */rt = dpi_prepare(hstmt, (sdbyte*)"insert into dpi_demo(c1,c2,c3,c4,c5,c6,c7 ) values(?,?,?,?,?,?,?)");DPIRETURN_CHECK(rt, DSQL_HANDLE_STMT, hstmt);/* 字段變量賦值 *///c1 = 201410;memcpy(c2, "abcde", 5);memcpy(c3, "abcdefghi", 9);c4 = 0.009;c5.year = 2011;c5.month = 3;c5.day = 1;c5.hour = 11;c5.minute = 45;c5.second = 50;c5.fraction = 900;memcpy(c6, "adfadsfetre2345ert", 18);memcpy(c7, "1234567890abcdef12", 18);c1_ind_ptr = sizeof(c1);c2_ind_ptr = 5; /* 獲取緩沖區長 度 */c3_ind_ptr = 9;c4_ind_ptr = sizeof(c4);c5_ind_ptr = sizeof(c5);c6_ind_ptr = 18;c7_ind_ptr = 18;/* 綁定參數 */rt = dpi_bind_param(hstmt, 1, DSQL_PARAM_INPUT, DSQL_C_SLONG, DSQL_INT, sizeof(c1), 0, &c1, sizeof(c1), &c1_ind_ptr);DPIRETURN_CHECK(rt, DSQL_HANDLE_STMT, hstmt);rt = dpi_bind_param(hstmt, 2, DSQL_PARAM_INPUT, DSQL_C_NCHAR, DSQL_CHAR, sizeof(c2), 0, c2, sizeof(c2), &c2_ind_ptr);DPIRETURN_CHECK(rt, DSQL_HANDLE_STMT, hstmt);rt = dpi_bind_param(hstmt, 3, DSQL_PARAM_INPUT, DSQL_C_NCHAR, DSQL_VARCHAR, sizeof(c3), 0, c3, sizeof(c3), &c3_ind_ptr);DPIRETURN_CHECK(rt, DSQL_HANDLE_STMT, hstmt);rt = dpi_bind_param(hstmt, 4, DSQL_PARAM_INPUT, DSQL_C_DOUBLE, DSQL_DOUBLE, sizeof(c4), 0, &c4, sizeof(c4), &c4_ind_ptr);DPIRETURN_CHECK(rt, DSQL_HANDLE_STMT, hstmt);rt = dpi_bind_param(hstmt, 5, DSQL_PARAM_INPUT, DSQL_C_TIMESTAMP, DSQL_TIMESTAMP, sizeof(c5), 0, &c5, sizeof(c5), &c5_ind_ptr);DPIRETURN_CHECK(rt, DSQL_HANDLE_STMT, hstmt);rt = dpi_bind_param(hstmt, 6, DSQL_PARAM_INPUT, DSQL_C_NCHAR, DSQL_CLOB, sizeof(c6), 0, c6, sizeof(c6), &c6_ind_ptr);DPIRETURN_CHECK(rt, DSQL_HANDLE_STMT, hstmt);rt = dpi_bind_param(hstmt, 7, DSQL_PARAM_INPUT, DSQL_C_NCHAR, DSQL_BLOB, sizeof(c7), 0, c7, sizeof(c7), &c7_ind_ptr);DPIRETURN_CHECK(rt, DSQL_HANDLE_STMT, hstmt);/* 執行 Dsql */rt = dpi_exec(hstmt);DPIRETURN_CHECK(rt, DSQL_HANDLE_STMT, hstmt);/* 釋放語句句柄 */rt = dpi_free_stmt(hstmt);DPIRETURN_CHECK(rt, DSQL_HANDLE_STMT, hstmt);printf("dm insert with bind param success\r\n");}/* fetch 獲取結果集*/ DPIRETURN dm_select_with_fetch() {sdint4 c1 = 0; /* 與字段匹配的變量,用于獲取字段值 */sdbyte c2[20];sdbyte c3[50];ddouble c4;dpi_timestamp_t c5;sdbyte c6[50];sdbyte c7[500];slength c1_ind = 0; /* 緩沖區 */slength c2_ind = 0;slength c3_ind = 0;slength c4_ind = 0;slength c5_ind = 0;slength c6_ind = 0;slength c7_ind = 0;ulength row_num; /* 行數 */sdint4 dataflag = 0;/* 分配語句句柄 */DPIRETURN_CHECK(dpi_alloc_stmt(hcon, &hstmt), DSQL_HANDLE_STMT, hstmt);/* 執行 sql 語句 */DPIRETURN_CHECK(dpi_exec_direct(hstmt,(sdbyte*)"select c1,c2,c3,c4,c5,c6,c7 from dpi_demo"), DSQL_HANDLE_STMT, hstmt);/* 綁定輸出列 */DPIRETURN_CHECK(dpi_bind_col(hstmt, 1, DSQL_C_SLONG, &c1, sizeof(c1), &c1_ind), DSQL_HANDLE_STMT, hstmt);DPIRETURN_CHECK(dpi_bind_col(hstmt, 2, DSQL_C_NCHAR, &c2, sizeof(c2), &c2_ind), DSQL_HANDLE_STMT, hstmt);DPIRETURN_CHECK(dpi_bind_col(hstmt, 3, DSQL_C_NCHAR, &c3, sizeof(c3), &c3_ind), DSQL_HANDLE_STMT, hstmt);DPIRETURN_CHECK(dpi_bind_col(hstmt, 4, DSQL_C_DOUBLE, &c4, sizeof(c4), &c4_ind), DSQL_HANDLE_STMT, hstmt);DPIRETURN_CHECK(dpi_bind_col(hstmt, 5, DSQL_C_TIMESTAMP, &c5, sizeof(c5), &c5_ind), DSQL_HANDLE_STMT, hstmt);DPIRETURN_CHECK(dpi_bind_col(hstmt, 6, DSQL_C_NCHAR, &c6, sizeof(c6), &c6_ind), DSQL_HANDLE_STMT, hstmt);DPIRETURN_CHECK(dpi_bind_col(hstmt, 7, DSQL_C_NCHAR, &c7, sizeof(c7), &c7_ind), DSQL_HANDLE_STMT, hstmt);printf("dm_select_with_fetch......\n");printf("----------------------------------------------------------------------\n");while (dpi_fetch(hstmt, &row_num) != DSQL_NO_DATA){printf("c1 = %d, c2 = %s, c3 = %s, c4 = %f, ", c1, c2, c3, c4);printf("c5 = %d - %d - %d %d:%d : %d.%d\n", c5.year, c5.month, c5.day, c5.hour, c5.minute, c5.second, c5.fraction);printf("c6 = %s, c7 = %s\n", c6, c7);dataflag = 1;}printf("----------------------------------------------------------------------\n");if (!dataflag){printf("dm no data\n");}/* 釋放語句句柄 */DPIRETURN_CHECK(dpi_free_stmt(hstmt), DSQL_HANDLE_STMT, hstmt);return DSQL_SUCCESS; }/* update數據*/ DPIRETURN dm_update_data(sdint4* old_c1, sdint4* new_c1) {//插入數據//sdint4 old_c1 = 0; /* 與字段匹配的變量 *///sdbyte new_c1 = 0;slength old_c1_ind_ptr; /* 緩沖區長度 */slength new_c1_ind_ptr; /* 緩沖區長度 *//* 分配語句句柄 */rt = dpi_alloc_stmt(hcon, &hstmt);DPIRETURN_CHECK(rt, DSQL_HANDLE_DBC, hcon);/* 準備 sql */rt = dpi_prepare(hstmt, (sdbyte*)"update dpi_demo set C1 = ? where C1 = ?");DPIRETURN_CHECK(rt, DSQL_HANDLE_STMT, hstmt);/* 字段變量賦值 */old_c1_ind_ptr = sizeof(old_c1);/* 獲取緩沖區長 度 */new_c1_ind_ptr = sizeof(new_c1);/* 獲取緩沖區長 度 *//* 綁定參數 */rt = dpi_bind_param(hstmt, 1, DSQL_PARAM_INPUT, DSQL_C_SLONG, DSQL_INT, sizeof(old_c1), 0, &old_c1, sizeof(old_c1), &old_c1_ind_ptr);DPIRETURN_CHECK(rt, DSQL_HANDLE_STMT, hstmt);rt = dpi_bind_param(hstmt, 2, DSQL_PARAM_INPUT, DSQL_C_SLONG, DSQL_INT, sizeof(new_c1), 0, &new_c1, sizeof(new_c1), &new_c1_ind_ptr);DPIRETURN_CHECK(rt, DSQL_HANDLE_STMT, hstmt);/* 執行 Dsql */rt = dpi_exec(hstmt);DPIRETURN_CHECK(rt, DSQL_HANDLE_STMT, hstmt);/* 釋放語句句柄 */rt = dpi_free_stmt(hstmt);DPIRETURN_CHECK(rt, DSQL_HANDLE_STMT, hstmt);printf("dm update data with param success\r\n");}/* update數據*/ DPIRETURN dm_delete_data(sdint4* data_id) {//插入數據//sdint4 old_c1 = 0; /* 與字段匹配的變量 *///sdbyte new_c1 = 0;slength data_id_ind_ptr; /* 緩沖區長度 *//* 分配語句句柄 */rt = dpi_alloc_stmt(hcon, &hstmt);DPIRETURN_CHECK(rt, DSQL_HANDLE_DBC, hcon);/* 準備 sql */rt = dpi_prepare(hstmt, (sdbyte*)"delete from DPI_DEMO where c1 =?");DPIRETURN_CHECK(rt, DSQL_HANDLE_STMT, hstmt);/* 字段變量賦值 */data_id_ind_ptr = sizeof(data_id);/* 獲取緩沖區長 度 *//* 綁定參數 */rt = dpi_bind_param(hstmt, 1, DSQL_PARAM_INPUT, DSQL_C_SLONG, DSQL_INT, sizeof(data_id), 0, &data_id, sizeof(data_id), &data_id_ind_ptr);DPIRETURN_CHECK(rt, DSQL_HANDLE_STMT, hstmt);/* 執行 Dsql */rt = dpi_exec(hstmt);DPIRETURN_CHECK(rt, DSQL_HANDLE_STMT, hstmt);/* 釋放語句句柄 */rt = dpi_free_stmt(hstmt);DPIRETURN_CHECK(rt, DSQL_HANDLE_STMT, hstmt);printf("dm delete data with id param success\r\n");}/* 斷開數據庫連接*/ DPIRETURN dm_dpi_disconnect() {/* 斷開連接 */rt = dpi_logout(hcon);DPIRETURN_CHECK(rt, DSQL_HANDLE_DBC, hcon);if (!DSQL_SUCCEEDED(rt)){dpi_err_msg_print(DSQL_HANDLE_DBC, hcon);return rt;}/* 釋放連接句柄和環境句柄 */rt = dpi_free_con(hcon);DPIRETURN_CHECK(rt, DSQL_HANDLE_DBC, hcon);rt = dpi_free_env(henv);DPIRETURN_CHECK(rt, DSQL_HANDLE_ENV, henv);return DSQL_SUCCESS; }void split(char str[], char delims[]) {char *result = NULL;result = strtok(str, delims);while (result != NULL) {printf("result is \"%s\"\n", result);result = strtok(NULL, delims);} } \/* 入口函數 */ int main(int argc, char* argv[]) {sdint4 out_c1 = 0;sdbyte out_c2[20] = { 0 };slength out_c1_ind = 0;slength out_c2_ind = 0;// 連接數據庫rt = dm_dpi_connect((sdbyte*)DM_SVR, (sdbyte*)DM_USER, (sdbyte*)DM_PWD);// 初始化環境rt = dm_init_env();// 插入數據rt = dm_insert_data((sdint4*)1);// 插入數據rt = dm_insert_data((sdint4*)2);// 插入數據rt = dm_insert_data((sdint4*)3);// 查詢數據rt = dm_select_with_fetch();//修改數據rt = dm_update_data((sdint4*)555, (sdint4*)1);// 查詢數據rt = dm_select_with_fetch();//刪除數據rt = dm_delete_data((sdint4*)2);// 查詢數據rt = dm_select_with_fetch();//斷開數據庫連接rt = dm_dpi_disconnect();system("pause");return 0;return rt; }運行結果
附錄
添加運行參數
調試==》命令參數SYSDBA/SYSDBA@127.0.0.1:5236 1 3
配置屬性里面沒有c/c++
使用VS建過C#的工程,再打開C++工程,新建一個項目,空工程,找不到配置屬性頁(可能也經歷過一些自動關鍵和斷電之類的異常情形,但不確定是導致bug的原因)。
解決方案
隨便編譯一次新的工程就好了
int main(){int i = 0;return 0; }達夢支持
=======================================
有任何問題請到技術社區反饋。
24小時免費服務熱線:400 991 6599
達夢技術社區:https://eco.dameng.com
總結
以上是生活随笔為你收集整理的Window下VS运行达梦DPI的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python连接DM8数据库
- 下一篇: Python对象基础