DELPHI 读 INI 文件连SQL SERVER,不知道为什么出错得很厉害解决方案
DELPHI 读 INI 文件连SQL SERVER,不知道为什么出错得很厉害
其实我是抄网上的,但不知道为什么错误得很严重
procedure TForm1.FormCreate(Sender: TObject);
begin
var
Ini_FileName:String;
Ini_FileName := ExtractFilePath(application.ExeName)+'hm.ini';
if not FileExists(Ini_FileName) then
begin
ShowMessage('数据库连接配置文件不存在!');
Application.Terimate;
end;
with ADOConnection1 do
Connection := False;
ConnectionString:=Get_Db_Con_Str(Ini_FileName);
try
Connection := True;
except
ShowMessage('数据库连接失败!请检查配置文件:'+Ini_FileName);
Application.Terimate;
end;
end;
function Get_Db_Con_Str(FileName:String):String;
var
ServerIP,SQLDBName,SQLUserName:string;
begin
Result := '';
with TInifile.Create(Filename) do
begin
try
ServerIP:=ReadString('Connect','ServerName','');
SQLDBName:=ReadString('Connect','Database','');
SQLUserName:=ReadString('Connect','LogId','');
finally
Free;
end;
Result := 'Provider=SQLOLEDB.1;Password='';Persist Security Info=True;User ID='+SQLUserName+';Initial Catalog='+SQLDBName+';Data Source='+ServerIP;
end;
end.
第一个 var 已经报错了
------解决方案--------------------
其实我是抄网上的,但不知道为什么错误得很严重
procedure TForm1.FormCreate(Sender: TObject);
begin
var
Ini_FileName:String;
Ini_FileName := ExtractFilePath(application.ExeName)+'hm.ini';
if not FileExists(Ini_FileName) then
begin
ShowMessage('数据库连接配置文件不存在!');
Application.Terimate;
end;
with ADOConnection1 do
Connection := False;
ConnectionString:=Get_Db_Con_Str(Ini_FileName);
try
Connection := True;
except
ShowMessage('数据库连接失败!请检查配置文件:'+Ini_FileName);
Application.Terimate;
end;
end;
function Get_Db_Con_Str(FileName:String):String;
var
ServerIP,SQLDBName,SQLUserName:string;
begin
Result := '';
with TInifile.Create(Filename) do
begin
try
ServerIP:=ReadString('Connect','ServerName','');
SQLDBName:=ReadString('Connect','Database','');
SQLUserName:=ReadString('Connect','LogId','');
finally
Free;
end;
Result := 'Provider=SQLOLEDB.1;Password='';Persist Security Info=True;User ID='+SQLUserName+';Initial Catalog='+SQLDBName+';Data Source='+ServerIP;
end;
end.
第一个 var 已经报错了
------解决方案--------------------
- Delphi(Pascal) code
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, DB, ADODB,IniFiles; type TForm1 = class(TForm) ADOConnection1: TADOConnection; procedure FormCreate(Sender: TObject); private function Get_Db_Con_Str(FileName:String):String; public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.FormCreate(Sender: TObject); var Ini_FileName:String; begin //放在BEGIN前面 Ini_FileName := ExtractFilePath(application.ExeName)+'hm.ini'; if not FileExists(Ini_FileName) then begin ShowMessage('数据库连接配置文件不存在!'); Application.Terminate;//Application.Terimate; end; with ADOConnection1 do begin Connected := False; //Connection ConnectionString:=Get_Db_Con_Str(Ini_FileName);//ConnectionString try Connected := True; except ShowMessage('数据库连接失败!请检查配置文件:'+Ini_FileName); Application.Terminate; end; end; end; function TForm1.Get_Db_Con_Str(FileName:String):String; var ServerIP,SQLDBName,SQLUserName:string; begin Result := ''; with TInifile.Create(Filename) do begin try ServerIP:=ReadString('Connect','ServerName',''); SQLDBName:=ReadString('Connect','Database',''); SQLUserName:=ReadString('Connect','LogId',''); finally Free; end; Result := 'Provider=SQLOLEDB.1;Password='';Persist Security Info=True;User ID='+SQLUserName+';Initial Catalog='+SQLDBName+';Data Source='+ServerIP; end; end; end.
------解决方案--------------------
var放在begin前面
------解决方案--------------------
procedure TForm1.FormCreate(Sender: TObject);
begin
var
Ini_FileName:String;
Ini_FileName := ExtractFilePath(application.ExeName)+'hm.ini';
if not FileExists(Ini_FileName) then
begin
ShowMessage('数据库连接配置文件不存在!');
Application.Terimate;
end;
with ADOConnection1 do
Connection := False;
ConnectionString:=Get_Db_Con_Str(Ini_FileName);
try