国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++ Builder動態(tài)設(shè)定odbc數(shù)據(jù)源

2019-09-06 23:33:31
字體:
供稿:網(wǎng)友

                    本文以access為例
需要文件 odbcinst.h(bcb或vc的include目錄)  odbccp32.dll(winntsystem32目錄) implib.exe(通過dll生成lib庫) coff2omf.exe (將coff庫轉(zhuǎn)化為omf庫)

首先將上面文件拷貝到工程目錄,
執(zhí)行下面命令:
implib.exe odbccp32.lib odbccp32.dll
coff2omf odbccp32.lib

下面程序的功能為當(dāng)應(yīng)用程序啟動時(shí),動態(tài)添加/修改一個(gè)指向當(dāng)前目錄的,名為test的系 統(tǒng)數(shù)據(jù)源。


新建一個(gè)工程,在工程主文件(含有winmain函數(shù))中添加
#include <odbcinst.h>
uselib("odbccp32.lib");//lib在工程當(dāng)前目錄



修改后的代碼如下;
//---------------------------------------------------------------------------

#include <vcl.h>
#include <odbcinst.h>

#pragma hdrstop
useform("main.cpp", form1);
//工程的其他單元申明.....
uselib("odbccp32.lib");
//---------------------------------------------------------------------------
winapi winmain(hinstance, hinstance, lpstr, int)
{
/ttry
/t{

/t/t application->initialize();
/t/t ansistring  data,datapath,dbpath;
/t/t unsigned char temp[255];//接收轉(zhuǎn)化后的連接字符串
/t/t lpstr ch;//字符串指針相當(dāng)于char *
/t/t datapath=getcurrentdir();//獲取當(dāng)前目錄/t      
/t/t data=datapath+"/test.mdb";//假定當(dāng)前數(shù)據(jù)庫為test.mdb
/t/t dbpath="dsn=testdbq="+data+"defaultdir="+datapath+"";
/t/t  //構(gòu)造odbc連接字符串,test為數(shù)據(jù)源名,非法字符先用代替
/t/t  
/t/t ch=dbpath.c_str();//將ansistring轉(zhuǎn)化為字符串
/t/t  int i=0;
/t/t while(*ch!='')//查找并替換非法字符,并裝入字符數(shù)組
/t/t {

/t/t  if(*ch=='')
/t/t   *ch='';
/t/t   temp=*ch;
/t/t   ch++;
/t/t   i++;
/t/t }
/t/t

/t/t if(::sqlconfigdatasource(null,odbc_add_sys_dsn,(lpstr)"microsoft access driver (*.mdb)",(lpstr)temp))//修改或添加數(shù)據(jù)源,如果成功,程序正常啟動
/t       {
/t/t
/t/t application->title = "動態(tài)設(shè)定odbc數(shù)據(jù)源";

/t/t application->createform(__classid(tform1), &form1);
/t/t
/t/t application->run();
/t/t }
/t/t else
/t/t {
/t/t  showmessage("數(shù)據(jù)庫設(shè)定失敗!");
/t/t  application->terminate();
/t/t }
/t}
/tcatch (exception &exception)
/t{
/t/t application->showexception(&exception);
/t}
/treturn 0;
}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 黎平县| 北流市| 沙河市| 革吉县| 思南县| 新和县| 通化市| 晋江市| 满洲里市| 河源市| 旅游| 江津市| 黎川县| 广西| 仙居县| 仁寿县| 青海省| 景泰县| 墨脱县| 阆中市| 嵊州市| 福建省| 加查县| 上饶市| 麦盖提县| 扎赉特旗| 安塞县| 娄底市| 兴隆县| 十堰市| 米脂县| 阿合奇县| 万安县| 昌邑市| 宜良县| 车致| 闵行区| 锦州市| 鞍山市| 本溪| 泰宁县|