//使用odbc協定
Action()
{
unsigned long i=0;
unsigned long * const count = &i;
{
unsigned long i=0;
unsigned long * const count = &i;
//1 初始化環境
lrd_init(&InitInfo, DBTypeVersion);
lrd_init(&InitInfo, DBTypeVersion);
//2 open context:Ctx1
// 初始化 LRD_CONTEXT structure
lrd_open_context(&Ctx1, LRD_DBTYPE_ODBC, 0, 0, 0);
// 初始化 LRD_CONTEXT structure
lrd_open_context(&Ctx1, LRD_DBTYPE_ODBC, 0, 0, 0);
//3 Allocates a connection
structure:con1
lrd_alloc_connection(&Con1, LRD_DBTYPE_ODBC, Ctx1, 0 /*Unused*/, 0);
lrd_alloc_connection(&Con1, LRD_DBTYPE_ODBC, Ctx1, 0 /*Unused*/, 0);
//4 Connects to the database.連結資料庫。
lrd_open_connection(&Con1, LRD_DBTYPE_ODBC, "", "", "","DRIVER=SQL Server;SERVER=;UID=;PWD=;DATABASE=", Ctx1, 1, 0);
lrd_open_connection(&Con1, LRD_DBTYPE_ODBC, "", "", "","DRIVER=SQL Server;SERVER=;UID=;PWD=;DATABASE=", Ctx1, 1, 0);
//5 open cursor
//the function(lrd_open_cursor) opens a cursor by setting up an LRD_CURSOR structure.
//You can use a single cursor to execute successive SQL statements
lrd_open_cursor(&Csr1, Con1, 0);
//the function(lrd_open_cursor) opens a cursor by setting up an LRD_CURSOR structure.
//You can use a single cursor to execute successive SQL statements
lrd_open_cursor(&Csr1, Con1, 0);
//7 插入一條記錄並返回該條記錄的關鍵值
lrd_cancel(0, Csr1, 0 /*Unused*/, 0);
lrd_stmt(Csr1,"INSERT INTO book1 (b) VALUES ('abc') Select IDENT_CURRENT('book1') \r\n", -1, 1 /*Direct exec*/, 0 /*None*/, 0);
lrd_cancel(0, Csr1, 0 /*Unused*/, 0);
lrd_stmt(Csr1,"INSERT INTO book1 (b) VALUES ('abc') Select IDENT_CURRENT('book1') \r\n", -1, 1 /*Direct exec*/, 0 /*None*/, 0);
//一定要使用下面的函數重設置LRD_CURSOR結構,否則在執行lrd_fetch時會提示“[ODBC SQL
Server Driver]無效的游標狀態”,
lrd_result_set(Csr1, 0, 0, 0);
//邦定列
lrd_bind_cols(Csr1, BCInfo_D39, 0);//其中BCInfo_D39在vdf.h中定義
//保存某行某列到變數中,在這個腳本中沒有什麼作用,只是為了體現這個函數的作用
lrd_save_col(Csr1,1,1,0,"Saved_number_D34");
//獲取所有查詢出來的值
lrd_fetch(Csr1, -8, 1, count, PrintRow3, 0);//其中PrintRow3在print.inl中定義,其中count為本次獲得的記錄的總行數
//保存當前行的某一個欄位的值到變數中(最後一行)
lrd_save_value(&a_D36, 0, 0, "emp_id");
lrd_result_set(Csr1, 0, 0, 0);
//邦定列
lrd_bind_cols(Csr1, BCInfo_D39, 0);//其中BCInfo_D39在vdf.h中定義
//保存某行某列到變數中,在這個腳本中沒有什麼作用,只是為了體現這個函數的作用
lrd_save_col(Csr1,1,1,0,"Saved_number_D34");
//獲取所有查詢出來的值
lrd_fetch(Csr1, -8, 1, count, PrintRow3, 0);//其中PrintRow3在print.inl中定義,其中count為本次獲得的記錄的總行數
//保存當前行的某一個欄位的值到變數中(最後一行)
lrd_save_value(&a_D36, 0, 0, "emp_id");
//8 根據lrd_save_value所取得的最後一行的a_D36欄位最為查詢準則,既所插入的值所返回的關鍵值
//釋放Csr後
lrd_cancel(0, Csr1, 0 /*Unused*/, 0);
lrd_stmt(Csr1,"select * from book1 where a = {emp_id} \r\n", -1, 1 /*Direct exec*/, 0 /*None*/, 0);
lrd_bind_cols(Csr1, BCInfo_D39, 0);
lrd_fetch(Csr1, -8, 1, count, PrintRow3, 0);
//9 close cusor、connectio、context
lrd_cancel(0, Csr1, 0 /*Unused*/, 0);
lrd_result_set(Csr1, 0, 0, 0);
lrd_db_option(Csr1, OT_ODBC_CURSOR_CLOSE, 0, 0);
lrd_db_option(Csr1, OT_ODBC_CURSOR_UNBOUNDCOLS, 0, 0);
lrd_close_cursor(&Csr1, 0);
lrd_close_connection(&Con1, 0, 0);
lrd_free_connection(&Con1, 0 /*Unused*/, 0);
lrd_close_context(&Ctx1, 0, 0);
}
lrd_cancel(0, Csr1, 0 /*Unused*/, 0);
lrd_stmt(Csr1,"select * from book1 where a = {emp_id} \r\n", -1, 1 /*Direct exec*/, 0 /*None*/, 0);
lrd_bind_cols(Csr1, BCInfo_D39, 0);
lrd_fetch(Csr1, -8, 1, count, PrintRow3, 0);
//9 close cusor、connectio、context
lrd_cancel(0, Csr1, 0 /*Unused*/, 0);
lrd_result_set(Csr1, 0, 0, 0);
lrd_db_option(Csr1, OT_ODBC_CURSOR_CLOSE, 0, 0);
lrd_db_option(Csr1, OT_ODBC_CURSOR_UNBOUNDCOLS, 0, 0);
lrd_close_cursor(&Csr1, 0);
lrd_close_connection(&Con1, 0, 0);
lrd_free_connection(&Con1, 0 /*Unused*/, 0);
lrd_close_context(&Ctx1, 0, 0);
}
沒有留言:
張貼留言