首页  编辑  

动态添加数据库DSN

Tags: /超级猛料/Database.数据库相关/安装、杂项和其他/   Date Created:

动态添加数据库DSN

unit odbcsetup;

//****************************//

// Written By Aaron Miles 2002        //

// Creates ODBC for Access           //

// Database                                      //

//**************************//

interface

uses sysutils, windows;

procedure CreateDSN(Name,Database:String);

procedure RemoveDSN(Name:String);

implementation

const

 ODBC_ADD_DSN = 1; // Add data source

 ODBC_CONFIG_DSN = 2; // Configure (edit) data

 ODBC_REMOVE_DSN = 3; // Remove data source

 ODBC_ADD_SYS_DSN = 4; // add a system DSN

 ODBC_CONFIG_SYS_DSN = 5; // Configure a system DSN

 ODBC_REMOVE_SYS_DSN = 6; // remove a system DSN

 ODBC_REMOVE_DEFAULT_DSN = 7; // remove the default DSN

function SQLConfigDataSource(

 hwndParent: HWND;

 fRequest: WORD;

 lpszDriver: LPCSTR;

 lpszAttributes: LPCSTR): BOOL; stdcall; external 'ODBCCP32.DLL';

procedure CreateDSN(Name,Database:String);

begin

 SQLConfigDataSource(0, ODBC_ADD_DSN,

   'Microsoft Access Driver (*.mdb)', PChar(

   'DSN='+Name+ #0 +

   'Driver=ODBCJT32.DLL'#0 +

   'DBQ='+Database+ #0 +

   'DefaultDir='+ExtractFilePath(Database)+ #0 +

   'Description=Auto Setup Aaron Miles'#0 +

   'FIL=MS Access'#0 +

   'UID=Admin'#0));

end;

procedure RemoveDSN(Name:String);

begin

 SQLConfigDataSource(0, ODBC_REMOVE_DSN,

   'Microsoft Access Driver (*.mdb)', PChar(

   'DSN='+Name+#0));

end;

end.