中国程序员联盟 正在重新改版中ing 不便之处还请见谅 改版后将内容涉及java delphi .net php
 
  首页 | 数据库开发 | 网络通讯 | 多线程 | 多媒体开发 | 图像处理 | 程序人生 | 系统函数 | 控件开发 | Web服务
 
  当前位置:笨鱼delphi技术网>数据库开发>开发应用>文章内容

delphi 添加ODBC System DSN

来源:站内 关于:bill 发布时间:2007-06-22   [收藏] [推荐]

下面这个例子显示了如何加载实现了ODBC 管理功能的DLL(odbccp32.dll)来创建一个Access MDB文件和指向它的ODBC DSN。注意:这里假设DLL和MDB都在当前目录下。
同时,"CREATE_DB"的调用是Access(MS Jet引擎)的专有调用,类似的还有COMPACT_DB和REPAIR_DB等。

const
  ODBC_ADD_DSN = 1; // 添加数据源
  ODBC_CONFIG_DSN = 2; // 配置数据源
  ODBC_REMOVE_DSN = 3; // 删除数据源
  ODBC_ADD_SYS_DSN = 4; // 添加系统DSN
  ODBC_CONFIG_SYS_DSN = 5; // 配置系统DSN
  ODBC_REMOVE_SYS_DSN = 6; // 删除系统DSN

type
  TSQLConfigDataSource = function( hwndParent: HWND;
  fRequest: WORD;
  lpszDriver: LPCSTR;
  lpszAttributes: LPCSTR ) : BOOL; stdcall;


procedure Form1.FormCreate(Sender: TObject);
var
  pFn: TSQLConfigDataSource;
  hLib: LongWord;
  strDriver: string;
  strHome: string;
  strAttr: string;
  strFile: string;
  fResult: BOOL;
  ModName: array[0..MAX_PATH] of Char;
  srInfo : TSearchRec;
begin
  Windows.GetModuleFileName( HInstance, ModName, SizeOf(ModName) );
  strHome := ModName;
  while ( strHome[length(strHome)] <> '\' ) do
  Delete( strHome, length(strHome), 1 );
  strFile := strHome + 'TestData.MDB'; // 检查Access权限(Axes = Access)
  hLib := LoadLibrary( 'ODBCCP32' );
  if( hLib <> NULL ) then
  begin
  @pFn := GetProcAddress( hLib, 'SQLConfigDataSource' );
  if( @pFn <> nil ) then
  begin
  // 强迫重建DSN
  strDriver := 'Microsoft Access Driver (*.mdb)';
  strAttr := Format( 'DSN=TestDSN'+#0+
  'DBQ=%s'+#0+
  'Exclusive=1'+#0+
  'Description=Test Data'+#0+#0,
  [strFile] );
  fResult := pFn( 0, ODBC_ADD_SYS_DSN, @strDriver[1], @strAttr[1] );
  if( fResult = false ) then ShowMessage( '创建DSN (Datasource)失败!' );

  // 检测/创建同DSN关联的 MDB 文件
  if( FindFirst( strFile, 0, srInfo ) <> 0 ) then
  begin
  strDriver := 'Microsoft Access Driver (*.mdb)';
  strAttr := Format( 'DSN=TestDSN'+#0+
  'DBQ=%s'+#0+
  'Exclusive=1'+#0+
  'Description=Test Data'+#0+
  'CREATE_DB="%s"'#0+#0,
  [strFile,strFile] );
  fResult := pFn( 0, ODBC_ADD_SYS_DSN, @strDriver[1], @strAttr[1] );
  if( fResult = false ) then ShowMessage( '创建MDB文件失败!' );
  end;
  FindClose( srInfo );

  end;

  FreeLibrary( hLib );
  end
  else
  begin
  ShowMessage( '无法加载ODBCCP32.DLL' );
  end;
end;


 

 
以下是在程序中动态创建ODBC的DSN数据源代码:
procedure TCreateODBCDSNfrm.CreateDSNBtnClick(Sender: TObject);
var
  registerTemp : TRegistry;
  bData : array[ 0..0 ] of byte;
begin
  registerTemp := TRegistry.Create;
  //建立一个Registry实例
  with registerTemp do
  begin
  RootKey:=HKEY_LOCAL_MACHINE;
  //设置根键值为HKEY_LOCAL_MACHINE
  //找到Software\ODBC\ODBC.INI\ODBC Data Sources
  if OpenKey('Software\ODBC\ODBC.INI
  \ODBC Data Sources',True) then
  begin //注册一个DSN名称
  WriteString( 'MyAccess', 'Microsoft
  Access Driver (*.mdb)' );
  end
  else
  begin//创建键值失败
  memo1.lines.add('增加ODBC数据源失败');
  exit;
  end;
  CloseKey;
//找到或创建Software\ODBC\ODBC.INI
 \MyAccess,写入DSN配置信息
  if OpenKey('Software\ODBC\ODBC.INI
  \MyAccess',True) then
  begin
  WriteString( 'DBQ', 'C:\inetpub\wwwroot
  \test.mdb' );//数据库目录,连接您的数据库
  WriteString( 'Description',
  '我的新数据源' );//数据源描述
  WriteString( 'Driver', 'C:\PWIN98\SYSTEM\
  odbcjt32.dll' );//驱动程序DLL文件
  WriteInteger( 'DriverId', 25 );
  //驱动程序标识
  WriteString( 'FIL', 'Ms Access;' );
  //Filter依据
  WriteInteger( 'SafeTransaction', 0 );
  //支持的事务操作数目
  WriteString( 'UID', '' );//用户名称
  bData[0] := 0;
  WriteBinaryData( 'Exclusive', bData, 1 );
  //非独占方式
  WriteBinaryData( 'ReadOnly', bData, 1 );
  //非只读方式
  end
  else//创建键值失败
  begin
  memo1.lines.add('增加ODBC数据源失败');
  exit;
  end;
  CloseKey;
//找到或创建Software\ODBC\ODBC.INI
\MyAccess\Engines\Jet
  //写入DSN数据库引擎配置信息
  if OpenKey('Software\ODBC\ODBC.INI
  \MyAccess\Engines\Jet',True) then
  begin
  WriteString( 'ImplicitCommitSync', 'Yes' );
  WriteInteger( 'MaxBufferSize', 512 );//缓冲区大小
  WriteInteger( 'PageTimeout', 10 );//页超时
  WriteInteger( 'Threads', 3 );//支持的线程数目
  WriteString( 'UserCommitSync', 'Yes' );
  end
  else//创建键值失败
  begin
  memo1.lines.add('增加ODBC数据源失败');
  exit;
  end;
  CloseKey;
  memo1.lines.add('增加新ODBC数据源成功');
  Free;
  end;
end;

从中您应该可以学会如何更改需要的数据库


[浏览: 次]   
上一篇:delphi 调出连接串编辑对话框   下一篇:delphi MDB数据库压缩方法
[收藏] [推荐] [返回顶部] [打印本页] [关闭窗口]  
    评论加载中…
google adsense热点文章
·delphi Delphi_三谈多态——善用virtua
·delphi 条形码处理
·delphi Delphi_三层开发基本概念介绍
·delphi 汉字转拼音码(上)
·delphi CS构架下的客户端自动更新程序
·delphi Olevariant
·delphi 在Dephi中使用TStream读写数据
·delphi delphi处理流
·delphi 汉字转拼音码(下)
·delphi 关于使用COM对象的方法
·delphi MTS组件——从理论到实践
·delphi 汉字转拼音码(中3)
     delphi技术网 | firefox 下载 | Avant Browser下载 | dedecms 技术网 | drupal 爱好者 | php 技术网
  Copyright@www.delphichm.com,2006-2009.All Rights Reserved.
 
程序员联盟 | delphi Java .net|QQ:707102932