Delphi用ini文件连接数据库

CS数据库连接-delphi+ini
chinayaosir

{delphi connection database with ini file}
unit DataModule;

interface

uses
  SysUtils, Classes, DB, ADODB,INIFiles;
type
  TDataModule1 = class(TDataModule)
  ADOConnection1: TADOConnection;
  procedure DataModuleCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  DataModule1: TDataModule1;

implementation
{$R *.dfm}
{
db.ini file
[connect]
servername=192.168.1.2
database=MRCRM
UserID=sa
Password=crm202.*-+ok
---------------------------------------------------
ado connection string
ADOConnection1.Close;
ADOConnection1.ConnectionString:='Provider=SQLOLEDB.1;Persist Security Info=True;'+
'User ID=sa;Password=crm202.*-+ok;Initial Catalog=CRM;Data Source=192.168.1.2';
ADOConnection1.Open;
}

procedure TDataModule1.DataModuleCreate(Sender: TObject) ;
var
inifile :TINIFile;
inipath,connstr:String;
ServerName,DbName,UserName,Pwd:String;
begin
  inipath:='db.ini';
  {ExtractFilePath(Application.ExeName)+'/db.ini' ;}
  inifile:=TIniFile.Create(inipath);
  try
    ServerName  := inifile.ReadString('connect','servername','');
    DbName      := inifile.ReadString('connect','database','');
    UserName    := inifile.ReadString('connect','UserID','');
    Pwd         := inifile.ReadString('connect','Password','');
  finally
    inifile.Free;
  end;      //end try

  try
    connstr:='Provider=SQLOLEDB.1;'+
    'Persist Security Info=False;'+
    'Auto Translate=True;'+
    'Data Source='+ServerName+';'+
    'Initial Catalog='+DbName+';'+
    'User ID='+UserName+';'+
    'Password='+PWD;
    ADOConnection1.Close;
    ADOConnection1.ConnectionString:=connstr;
    ADOConnection1.Open;
  except
    //ShowMessage('init database fail,pls check!');
  end;     //try end

end;     //procedure end

end.  //program end

你可能感兴趣的:(crm,Security,database,ini,Delphi)