动态创建连接ODBC SQL数据库

unit Unit1;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,  StdCtrls,Registry, Buttons, ExtCtrls;
type
  TLinkDB_info = class(TForm)
    Memo1: TMemo;
    pnllink: TPanel;
    pnlclose: TPanel;
    procedure btn2Click(Sender: TObject);
    procedure pnllinkClick(Sender: TObject);
    procedure pnlcloseClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  LinkDB_info: TLinkDB_info;
const  databaseName='SetCalc';   //数据库名 (SQL中的)
       ODBCName='PowerDB';       //ODBC数据源名
       Aboutname='发电厂继电保护整定综合管理系统';//数据源描述
implementation
{$R *.dfm}
procedure TLinkDB_info.btn2Click(Sender: TObject);
begin
close;
end;
procedure TLinkDB_info.pnllinkClick(Sender: TObject);
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( ODBCName,'SQL Server');
end
else
begin//创建键值失败
memo1.lines.add('连接ODBC数据源失败');
exit;
end;
CloseKey;          
//找到或创建Software\ODBC\ODBC.INI\MGMIS_XDB,写入DSN配置信息
if OpenKey('Software\ODBC\ODBC.INI\'+ODBCName,True) then
begin
WriteString( 'Database', databaseName);//数据库
WriteString( 'Description', Aboutname );//数据源描述
WriteString('Driver','D:\WINDOWS\system32\sqlsrv32.dll' );//驱动程序DLL文件
Writestring( 'LastUser', 'klx');
WriteString( 'Server','(local)' );//Filter依据
Writestring( 'Trusted_Connection', 'Yes' );//支持的事务操作数目
end
else//创建键值失败
begin
memo1.lines.add('连接ODBC数据源失败');
exit;
end;
CloseKey;
CloseKey;
memo1.lines.add('连接ODBC数据源成功');
Free;
end;
end;

procedure TLinkDB_info.pnlcloseClick(Sender: TObject);
begin
Close ;
end;
procedure TLinkDB_info.FormCreate(Sender: TObject);
begin
Self.Caption:=Aboutname;
end;
end.
 

你可能感兴趣的:(sql,windows,SQL Server,配置管理)