home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 March / Chip_2002-03_cd1.bin / zkuste / delphi / kompon / d5 / ADO.ZIP / src / AdoUtils.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2001-12-23  |  5.6 KB  |  181 lines

  1. unit AdoUtils;
  2.  
  3. interface
  4.  
  5. uses
  6.   OleCtrls, SysUtils, Classes, Registry, Windows,
  7.   Ado21Int, MsDascInt;
  8.  
  9. procedure OleEnumExclude(Value: TOleEnum; var EnumSet: TOleEnum);
  10. procedure OleEnumInclude(Value: TOleEnum; var EnumSet: TOleEnum);
  11. function OleEnumInSet(Value, EnumSet: TOleEnum): boolean;
  12. function OleEnumToOrd(OleEnumArray: array of TOleEnum; Value: TOleEnum): Integer;
  13.  
  14. // this code is created by Copyright Aloha Oi Software
  15. // http://www.alohaoi.com/Software
  16. procedure GetDrivers(Drivers: TStrings);
  17. procedure GetDSNs(DSNs: TStrings; SystemDSN, UserDSN: Boolean);
  18. procedure GetProviderNames(Providers: TStrings);
  19. function ConnectionStringDlg(Previous: String): String;
  20.  
  21. implementation
  22. //------------------------------------------------------------------------------
  23. function OleEnumInSet(Value, EnumSet: TOleEnum): boolean;
  24. begin
  25.   result:= (EnumSet and Value) = Value;
  26. end;
  27. //------------------------------------------------------------------------------
  28. procedure OleEnumInclude(Value: TOleEnum; var EnumSet: TOleEnum);
  29. begin
  30.   if not OleEnumInSet(Value, EnumSet) then
  31.     EnumSet:= EnumSet or Value;
  32. end;
  33. //------------------------------------------------------------------------------
  34. procedure OleEnumExclude(Value: TOleEnum; var EnumSet: TOleEnum);
  35. begin
  36.   if OleEnumInSet(Value, EnumSet) then
  37.     EnumSet:= EnumSet xor Value;
  38. end;
  39. //------------------------------------------------------------------------------
  40. function OleEnumToOrd(OleEnumArray: array of TOleEnum; Value: TOleEnum): Integer;
  41. begin
  42.   for Result := Low(OleEnumArray) to High(OleEnumArray) do
  43.     if Value = OleEnumArray[Result] then Exit;
  44.   raise Exception.Create('Invalid Enum Value');
  45. end; 
  46. //------------------------------------------------------------------------------
  47. {This procedure will return all driver names on the system. This procedure
  48. works directly on the registry rather than through ADO or ODBC.}
  49. procedure GetDrivers(Drivers: TStrings);
  50. var
  51.   i  : Integer;
  52.   Reg: TRegistry;
  53. begin
  54.   Drivers.Clear;
  55.   {Create the registry link.}
  56.   Reg:= TRegistry.Create;
  57.   Reg.RootKey:= HKEY_LOCAL_MACHINE;
  58.   {$IFNDEF OLD_STYLE}
  59.   Reg.Access:= KEY_READ;
  60.   {$ENDIF}
  61.   Reg.OpenKey('\SOFTWARE\ODBC\odbcinst.ini\ODBC Drivers', False);
  62.   Reg.GetValueNames(Drivers);
  63.   i:= 0;
  64.   while i < Drivers.Count do
  65.     if UpperCase(Reg.ReadString(Drivers[i])) = 'INSTALLED' then inc(i)
  66.     else Drivers.Delete(i);
  67.   Reg.Free;
  68. end;
  69. //------------------------------------------------------------------------------
  70. {This procedure will return all DSN names on the system. This procedure
  71. works directly on the registry rather than through ADO or ODBC. Specify
  72. which sets of DSN you want to retrieve - System and/or User. If value
  73. is specified, then no DSNs will be retieved.}
  74. procedure GetDSNs(DSNs: TStrings; SystemDSN, UserDSN: Boolean);
  75. var
  76.   L  : TStrings;
  77.   Reg: TRegistry;
  78. begin
  79.   DSNs.Clear;
  80.   {Create the registry link.}
  81.   Reg:= TRegistry.Create;
  82.   L:= TStringList.Create;
  83.   try
  84.     {$IFNDEF OLD_STYLE}
  85.     Reg.Access:= KEY_READ;
  86.     {$ENDIF}
  87.  
  88.     {Get the system DSN. These DSN are available to all users.}
  89.     if SystemDSN then
  90.       begin
  91.         Reg.RootKey:= HKEY_LOCAL_MACHINE;
  92.         Reg.OpenKey('\SOFTWARE\ODBC\odbc.ini\ODBC Data Sources', False);
  93.         Reg.GetValueNames(L);
  94.         DSNs.AddStrings(L);
  95.         L.Clear;
  96.       end;
  97.  
  98.     {Get the user DSN. These DSN are available only to the user.}
  99.     if UserDSN then
  100.       begin
  101.         Reg.RootKey:= HKEY_CURRENT_USER;
  102.         Reg.OpenKey('\SOFTWARE\ODBC\odbc.ini\ODBC Data Sources', False);
  103.         Reg.GetValueNames(L);
  104.         DSNs.AddStrings(L);
  105.         L.Clear;
  106.       end;
  107.   finally
  108.     Reg.Free;
  109.     L.Free;
  110.   end;
  111. end; 
  112. //------------------------------------------------------------------------------
  113. {This method will return all OLE DB Providers listed in the system. This
  114. method does not require that a connection be established, since it works
  115. directly on the registry rather than through ADO or ODBC.}
  116. procedure GetProviderNames(Providers: TStrings);
  117. var
  118.   i: Integer;
  119.   L: TStringList;
  120.   R: TRegistry;
  121. begin
  122.   Providers.Clear;
  123.   {Open the registry.}
  124.   R:= TRegistry.Create;
  125.   L:= TStringList.Create;
  126.   try
  127.     R.RootKey:= HKEY_CLASSES_ROOT;
  128.     {$IFNDEF OLD_STYLE}
  129.     R.Access:= KEY_READ;
  130.     {$ENDIF}
  131.     if R.OpenKey('\CLSID', False) then
  132.       begin
  133.         R.GetKeyNames(L);
  134.         for i:= 0 to L.Count - 1 do
  135.           if R.OpenKey('\CLSID\' + L[i], False) then
  136.             begin
  137.               if R.KeyExists('OLE DB Provider') then
  138.                 Providers.Add(R.ReadString(''));
  139.             end;
  140.       end;
  141.   finally
  142.     R.Free;
  143.     L.Free;
  144.   end;
  145. end;    
  146. //------------------------------------------------------------------------------
  147. {This method will return the constructed connection string after presenting
  148. the ADO dialog box. The Previous paramater is used to open the dialog box
  149. on the specified connection string.}
  150. function ConnectionStringDlg(Previous: String): String;
  151. var
  152.   Con: _Connection;
  153.   DL : Datalinks;
  154.   ID : IDispatch;
  155. begin
  156.   {Get the actual dataset.}
  157.   Result:= Previous;
  158.   DL:= CoDatalinks.Create;
  159.   Con:= CoConnection.Create;
  160.   DL.Set_hwnd(0);
  161.   try
  162.     if Previous = '' then
  163.       begin
  164.          Con:= _Connection(DL.PromptNew);
  165.          if Assigned(Con) then Result:= Con.ConnectionString;
  166.       end
  167.     else
  168.       begin
  169.          Con.ConnectionString:= Previous;
  170.          ID:= IDispatch(Con);
  171.          if DL.PromptEdit(ID) then Result:= Con.ConnectionString;
  172.       end;
  173.   finally
  174.      DL:= NIL;
  175.      Con:= NIL;
  176.      ID:= NIL;
  177.   end;
  178. end;
  179.  
  180. end.
  181.