这是delphi里一个创建sqlserver数据库odbc系统dsn别名的过程,
别名和数据库名相同.
procedure CreateSqlDsn(DatabaseName:string);
var
DriverAddr :string;
DsnAliasName :string;
Hostname :string;
name
char;
size :dword;
ARegistry :TRegistry;
begin
size:=255;
getmem(name,size);
getcomputername(name,size);
hostname:=name;
freemem(name);
ARegistry := TRegistry.Create;
with ARegistry do
{创建一个TRegistry对象实例 }
begin
RootKey := HKEY_LOCAL_MACHINE;//读sql server 数据库配置
if OpenKey( 'SOFTWARE/ODBC/ODBCINST.INI/SQL Server', False ) then
begin
driveraddr:=ReadString('Driver');
end
else
begin
CloseKey;
exit;
end;
CloseKey;
RootKey := HKEY_LOCAL_MACHINE;//写dsn配置
if not OpenKey( 'SOFTWARE/ODBC/ODBC.INI/'+databasename, False ) then
begin
OpenKey( 'SOFTWARE/ODBC/ODBC.INI/'+databasename, true);
//~~~~~~~~~~~~别名
WriteString('Driver',driveraddr);
WriteString('Server',hostname);
WriteString('Database',databasename);
WriteString('LastUser','sa');
end;
CloseKey;
RootKey := HKEY_LOCAL_MACHINE;//添加别名
if OpenKey( 'SOFTWARE/ODBC/ODBC.INI/ODBC Data Sources', False ) then
begin
if not ValueExists(databasename) then
WriteString(databasename,'SQL Server');
end;
CloseKey;
end;
end;