首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

DELPHI 读 INI 文件连SQL SERVER,不知道为什么出错得很厉害解决方案

2012-04-18 
DELPHI 读 INI 文件连SQL SERVER,不知道为什么出错得很厉害其实我是抄网上的,但不知道为什么错误得很严重p

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 已经报错了

[解决办法]

Delphi(Pascal) code
unit Unit1;interfaceuses  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


Connection := True;

except
ShowMessage('数据库连接失败!请检查配置文件:'+Ini_FileName);
Application.Terimate;
end;

end;

---->>>>

Delphi(Pascal) code
procedure TForm1.FormCreate(Sender: TObject);var  Ini_FileName:String;begin  Ini_FileName := ExtractFilePath(application.ExeName)+'hm.ini';  if not FileExists(Ini_FileName) then  begin    ShowMessage('数据库连接配置文件不存在!');    Application.Terminate;  end;  with ADOConnection1 do  begin    Connected := False;    ConnectionString:=Get_Db_Con_Str(Ini_FileName);    try    Connected := True;    except      ShowMessage('数据库连接失败!请检查配置文件:'+Ini_FileName);      Application.Terminate;    end;  end;end; 

热点排行