Automatically create ODBC data sources (access, SQL Server, DB2

Source: Internet
Author: User
Tags db2 driver db2 odbc driver dsn ibm db2 ibm db2 odbc driver sql server driver

{--------------------------------------------------------------------}
{}
{Unit name: createodbc}
{}
{Author: Ji Tao}
{}
{Version: V1.0}
{}
{Date: 2006-7-9}
{}
{Purpose and Description: automatically create data sources for accsee, SQL Server, DB2, and 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;
// First, determine whether the DSN already exists. If so, you do not need to continue.
If openkey ('Software/ODBC. INI/'+ fdsn, false) then
Begin
Closekey;
Result: = true;
Free;
Exit;
End;
Closekey;
// Determine whether the SQL Server Driver has been installed
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;
// Find the SQL Server Driver path
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;
// Register a DSN name
If openkey ('Software/ODBC. INI/ODBC Data Sources ', true) then
Writestring (fdsn, 'SQL Server ')
Else
Begin
Closekey;
Result: = true;
Free;
Exit;
End;
Closekey;
If openkey ('Software/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; // create a registry instance
With registertemp do
Begin
Rootkey: = HKEY_LOCAL_MACHINE; // you can specify HKEY_LOCAL_MACHINE as the root key.

// Find software/ODBC. INI/ODBC Data sources
If openkey ('Software/ODBC. INI/ODBC Data Sources ', true) then
Begin // register a DSN name
Writestring (mydsn, 'Microsoft access Driver (*. mdb )');
End
Else
Begin // failed to create the key value
Result: = false;
Exit;
End;
Closekey;

// Locate or create software/ODBC. INI/myaccess and write the DSN configuration information
If openkey ('Software/ODBC. INI/'+ mydsn, true) then
Begin
Writestring ('dbq', strfilename); // database directory
Writestring ('description', 'My new source'); // data source description
Writestring ('driver ', 'c:/winnt/system32/odbcjt32.dll'); // driver DLL file
Writeinteger ('driverid', 25); // driver ID
Writestring ('fil ', 'Ms access;'); // filter basis
Writeinteger ('Safety action', 0); // the number of supported transaction operations
Writestring ('uid', ''); // User Name
Bdata [0]: = 0;
Writebinarydata ('exclusive ', bdata, 1); // non-exclusive mode
Writebinarydata ('readonly', bdata, 1); // non-read-only mode
End
Else // failed to create the key value
Begin
Result: = false;
Exit;
End;
Closekey;
// Locate or create software/ODBC. INI/myaccess/engines/Jet
// Write the configuration information of the DSN Database Engine
If openkey ('Software/ODBC. INI/'+ mydsn +'/engines/jet ', true) then
Begin
Writestring ('implicitcommitsync', 'yes ');
Writeinteger ('maxbuffersize', 512); // buffer size
Writeinteger ('pagetimeout', 10); // page timeout
Writeinteger ('threads', 8); // number of supported threads
Writestring ('usercommitsync', 'yes ');
End
Else // failed to create the key value
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;
// First, determine whether the DSN already exists. If so, you do not need to continue.
If openkey ('Software/ODBC. INI/'+ fdsn, false) then
Begin
Closekey;
Result: = true;
Free;
Exit;
End;
Closekey;
// Determine whether the DB2 driver has been installed
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;
// Find the DB2 driver path
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;
// Register a DSN name
If openkey ('Software/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. 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;

// First, determine whether the DSN already exists. If so, you do not need to continue.
If registertemp. openkey ('Software/ODBC. INI/'+ fdsn, false) then
Begin
Registertemp. closekey;
Result: = true;
Registertemp. Free;
Exit;
End;
Registertemp. closekey;
// Determine whether the Oracle driver has been installed
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;
// Find the path of the Oracle driver
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;
// Register a DSN name
If registertemp. openkey ('Software/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. INI/'+ fdsn, true) then
Begin
Registertemp. writestring ('driver ', S );
Registertemp. writestring ('userid', Fuser );
Registertemp. writestring ('servername', fserver );
Registertemp. writestring ('dns', fdsn );
Registertemp. writestring ('application bubutes ', 'T ');
Registertemp. writestring ('bubuckets', '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 ('failback', '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.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.