自動建立ODBC資料來源(Access,Sql Server,DB2

來源:互聯網
上載者:User

{--------------------------------------------------------------------}
{                                                                    }
{   單元名:    CreateODBC                                           }
{                                                                    }
{   作者:      冀濤                                              }
{                                                                    }
{   版本:      V1.0                                                 }
{                                                                    }
{   日期:      2006-7-9                                             }
{                                                                    }
{   用途與描述:自動建立Accsee、Sql Server、DB2、Oracle的資料來源      }
{                                                                    }
{--------------------------------------------------------------------}

unit CreateODBC;

interface
uses
  Windows,
  SysUtils,
  Registry,
  Classes;

function CreateSQLServerDSN(const FDSN, FServer, FUser: string): boolean;
function CreateAccessDSN(const myDSN, strFileName: string): boolean;
function CreateDB2DSN(const FDSN: string): Boolean;
function CreateOracleDSN(const FDSN,FServer,Fuser:string):boolean;

imple****tion

function CreateSQLServerDSN(const FDSN, FServer, FUser: string): boolean;
var
  RegisterTemp: TRegistry;
  s: string;
begin
  RegisterTemp := TRegistry.Create;
  with RegisterTemp do
  begin
    RootKey := HKEY_LOCAL_MACHINE;
    //首先判斷此DSN是否已經存在,如在,則不用繼續下去
    if OpenKey('Software/ODBC/ODBC.INI/' + FDSN, False) then
    begin
      CloseKey;
      Result := True;
      Free;
      exit;
    end;
    CloseKey;
    //判斷SQL Server的驅動程式是否已經安裝
    if OpenKey('Software/ODBC/ODBCINST.INI/ODBC Drivers', False) then
    begin
      s := ReadString('SQL Server');
      if UpperCase(S) <> 'INSTALLED' then
      begin
        CloseKey;
        Result := False;
        Free;
        exit;
      end;
    end
    else
    begin
      CloseKey;
      Result := True;
      Free;
      exit;
    end;
    CloseKey;
    //尋找SQL Server的驅動程式路徑
    if OpenKey('Software/ODBC/ODBCINST.INI/SQL Server', False) then
    begin
      s := ReadString('Driver');
      if S = '' then
      begin
        CloseKey;
        Result := False;
        Free;
        exit;
      end;
    end
    else
    begin
      CloseKey;
      Result := True;
      Free;
      exit;
    end;
    CloseKey;
    //註冊一個DSN名稱
    if OpenKey('Software/ODBC/ODBC.INI/ODBC Data Sources', True) then
      WriteString(FDSN, 'SQL Server')
    else
    begin
      CloseKey;
      Result := True;
      Free;
      exit;
    end;
    CloseKey;
    if OpenKey('Software/ODBC/ODBC.INI/' + FDSN, True) then
    begin
      WriteString('Driver', S);
      WriteString('LastUser', FUser);
      WriteString('Server', FServer);
      Result := True;
    end
    else
    begin
      CloseKey;
      Result := False;
      Free;
      exit;
    end;
    CloseKey;
    Free;
  end;
end;

function CreateAccessDSN(const myDSN, strFileName: string): boolean;
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(myDSN, 'Microsoft Access Driver (*.mdb)');
    end
    else
    begin //建立索引值失敗
      Result := False;
      exit;
    end;
    CloseKey;

    //找到或建立Software/ODBC/ODBC.INI/MyAccess,寫入DSN配置資訊
    if OpenKey('Software/ODBC/ODBC.INI/' + MyDSN, True) then
    begin
      WriteString('DBQ', strFileName); //資料庫目錄
      WriteString('Description', '我的新資料來源'); //資料來源描述
      WriteString('Driver', 'C:/WINNT/system32/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
      Result := False;
      exit;
    end;
    CloseKey;
    //找到或建立Software/ODBC/ODBC.INI/MyAccess/Engines/Jet
    //寫入DSN資料庫引擎配置資訊
    if OpenKey('Software/ODBC/ODBC.INI/' + MyDSN + '/Engines/Jet', True) then
    begin
      WriteString('ImplicitCommitSync', 'Yes');
      WriteInteger('MaxBufferSize', 512); //緩衝區大小
      WriteInteger('PageTimeout', 10); //頁逾時
      WriteInteger('Threads', 8); //支援的線程數目
      WriteString('UserCommitSync', 'Yes');
    end
    else //建立索引值失敗
    begin
      Result := False;
      exit;
    end;
    CloseKey;
    Result := True;
    Free;
  end;
end;

function CreateDB2DSN(const FDSN: string): Boolean;
var
  RegisterTemp: TRegistry;
  s: string;
begin
  RegisterTemp := TRegistry.Create;
  with RegisterTemp do
  begin
    RootKey := HKEY_LOCAL_MACHINE;
    //首先判斷此DSN是否已經存在,如在,則不用繼續下去
    if OpenKey('Software/ODBC/ODBC.INI/' + FDSN, False) then
    begin
      CloseKey;
      Result := True;
      Free;
      exit;
    end;
    CloseKey;
    //判斷DB2的驅動程式是否已經安裝
    if OpenKey('Software/ODBC/ODBCINST.INI/ODBC Drivers', False) then
    begin
      s := ReadString('IBM DB2 ODBC DRIVER');
      if UpperCase(S) <> 'INSTALLED' then
      begin
        CloseKey;
        Result := False;
        Free;
        exit;
      end;
    end
    else
    begin
      CloseKey;
      Result := True;
      Free;
      exit;
    end;
    CloseKey;
    //尋找DB2的驅動程式路徑
    if OpenKey('Software/ODBC/ODBCINST.INI/IBM DB2 ODBC DRIVER', False) then
    begin
      s := ReadString('Driver');
      if S = '' then
      begin
        CloseKey;
        Result := False;
        Free;
        exit;
      end;
    end
    else
    begin
      CloseKey;
      Result := True;
      Free;
      exit;
    end;
    CloseKey;
    //註冊一個DSN名稱
    if OpenKey('Software/ODBC/ODBC.INI/ODBC Data Sources', True) then
      WriteString(FDSN, 'IBM DB2 ODBC DRIVER')
    else
    begin
      CloseKey;
      Result := True;
      Free;
      exit;
    end;
    CloseKey;
    if OpenKey('Software/ODBC/ODBC.INI/' + FDSN, True) then
    begin
      WriteString('Driver', S);
      Result := True;
    end
    else
    begin
      CloseKey;
      Result := False;
      Free;
      exit;
    end;
    CloseKey;
    Free;
  end;
end;

function createOracleDSN(const FDSN,FServer,Fuser:string):boolean;
var
  RegisterTemp:TRegistry;
  s:string;
  ODBCDriverNameList:TStringList;
  OraDriverName:string;
  i:Integer;
begin
RegisterTemp:=Tregistry.Create;

RegisterTemp.RootKey:=HKEY_LOCAL_MACHINE;

//首先判斷此DSN是否已經存在,如在,則不用繼續下去
if registertemp.OpenKey('Software/ODBC/ODBC.INI/' + FDSN, False) then
   begin
        RegisterTemp.CloseKey;
        Result := True;
        RegisterTemp.Free;
        exit;
  end;
RegisterTemp.CloseKey;
//判斷Oracle的驅動程式是否已經安裝
if RegisterTemp.OpenKey('Software/ODBC/ODBCINST.INI/ODBC Drivers', False) then
  begin
        ODBCDriverNameList:=TStringList.Create;
        RegisterTemp.GetValueNames(ODBCDriverNameList);
        for i:=1 to ODBCDriverNameList.Count-1 do
        begin
          if AnsiPos('Oracle in OraHome',ODBCDriverNameList.Strings[i])<>0 then
          begin
            OraDriverName:=ODBCDriverNameList.Strings[i];
            Break;
          end;
        end;
        s := RegisterTemp.ReadString(OraDriverName);
        if UpperCase(S) <> 'INSTALLED' then
          begin
          RegisterTemp.CloseKey;
          Result := False;
          RegisterTemp.Free;
          exit;
          end;
  end
  else
  begin
    RegisterTemp.CloseKey;
    Result := True;
    RegisterTemp.Free;
    exit;
  end;
  RegisterTemp.CloseKey;
//尋找oracle的驅動程式路徑
if RegisterTemp.OpenKey('Software/ODBC/ODBCINST.INI/'+OraDriverName, False) then
begin
  s := RegisterTemp.ReadString('Driver');
  if S = '' then
  begin
    RegisterTemp.CloseKey;
    Result := False;
    RegisterTemp.Free;
    exit;
  end;
end
else
begin
  RegisterTemp.CloseKey;
  Result := True;
  RegisterTemp.Free;
  exit;
end;
RegisterTemp.CloseKey;
//註冊一個DSN名稱
if RegisterTemp.OpenKey('Software/ODBC/ODBC.INI/ODBC Data Sources', True) then
  RegisterTemp.WriteString(FDSN, OraDriverName)
else
begin
  RegisterTemp.CloseKey;
  Result := True;
  RegisterTemp.Free;
  exit;
end;
RegisterTemp.CloseKey;
if RegisterTemp.OpenKey('Software/ODBC/ODBC.INI/' + FDSN, True) then
begin
  RegisterTemp.WriteString('Driver', S);
  RegisterTemp.WriteString('UserID', FUser);
  RegisterTemp.WriteString('ServerName', FServer);
  RegisterTemp.WriteString('DNS', FDSN);
  RegisterTemp.WriteString('Application Attributes','T');
  RegisterTemp.WriteString('Attributes','W');
  RegisterTemp.WriteString('BatchAutocommitMode','IfAllSuccessful');
  RegisterTemp.WriteString('CloseCursor','F');
  RegisterTemp.WriteString('Description','');
  RegisterTemp.WriteString('DisableMTS','F');
  RegisterTemp.WriteString('EXECSchemaOpt','');
  RegisterTemp.WriteString('EXECSyntax','F');
  RegisterTemp.WriteString('Failover','T');
  RegisterTemp.WriteString('FailoverDelay','10');
  RegisterTemp.WriteString('FailoverRetryCount','10');
  RegisterTemp.WriteString('ForceWCHAR','F');
  RegisterTemp.WriteString('Lobs','T');
  RegisterTemp.WriteString('Longs','F');
  RegisterTemp.WriteString('****dataIdDefault','T');
  RegisterTemp.WriteString('Password','');
  RegisterTemp.WriteString('PrefetchCount','10');
  RegisterTemp.WriteString('QueryTimeout','T');
  RegisterTemp.WriteString('ResultSets','T');
  RegisterTemp.WriteString('Translation DLL','');
  RegisterTemp.WriteString('Translation Option','0');

  Result := True;
end
else
begin
  RegisterTemp.CloseKey;
  Result := False;
  RegisterTemp.Free;
  exit;
end;
RegisterTemp.CloseKey;
RegisterTemp.Free;

end;

end.

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.