急需各位大虾的帮忙,俺遇到一个问题,怎么在程序开始打开数据库,在主窗体的onshow事件中连接数据库?需要什么步骤?在此恭送200大分,不好意思以后补,我好像不能送出这么多分数
wljcr 2001-05-24 09:47:00 急需各位大虾的帮忙,俺遇到一个问题,怎么在程序开始打开数据库,在主窗体的onshow事件中连接数据库?需要什么步骤?源程序的部分代码如下:请各位赐教,有高分相送
procedure Tfrmmain.newINIFile;
var
filename:string;
Tmp:Tstringlist;
begin
filename:='user.ini';
if not FileExists(filename) then
begin
Tmp := TStringList.Create; //ado连接
Tmp.Add('Provider=SQLOLEDB.1;');
Tmp.Add('Persist Security Info=False;');
Tmp.Add('Initial Catalog=inspection;');
Tmp.Add('Data Source=195.0.1.24;');
Tmp.Add('Use Procedure for Prepare=1;');
Tmp.Add('Auto Translate=True;');
Tmp.Add('Packet Size=4096;');
Tmp.Add('User ID=sa;');
Tmp.SaveToFile(filename);
Tmp.Free;
end;
if not FileExists(filename) then //存取用户名
begin
Tmp := TStringList.Create;
Tmp.add('123456');
tmp.SaveToFile(filename);
end;
end;
function Tfrmmain.newlogin:boolean;
var
tmpStr:TstringList; //用来存取数据库连接的connectionstring;
tmpUser:Tstringlist;
begin
result:=false;
tmpstr:=Tstringlist.Create;
TmpUser:=TstringList.create;
Tmpuser.clear;
tmpstr.Clear;
newINIfile;
tmpstr.LoadFromFile('inspection.ini');
if not assigned(logform) then
logform:=Tlogform.Create(self); //logform为登陆窗口
tmpuser.LoadFromFile('user.ini');
logform.Edit1.Text:=Tmpuser.Strings[0];
logform.Edit2.Text:='';
if logform.ShowModal=mrok then
begin
edit1.Text:=logform.Edit1.Text;
edit2.Text:=logform.Edit2.Text;
TmpUser.Clear;
TmpUser.Add(logform.edit1.text);
tmpuser.SaveToFile('user.ini');
customerdata.SYSConnect.Close;
customerdata.SYSConnect.ConnectionString:=TmpStr.Text;
try
customerdata.SYSConnect.Open;
customerdata.SYSConnect.Close;
customerdata.inspectionconnect.Close;
customerdata.inspectionconnect.ConnectionString:=TmpStr.Text;
customerdata.inspectionconnect.Open;
TmpUser.Clear;
TmpUser.Add(logform.edit1.text);
tmpuser.SaveToFile('user.ini');
result:=true;
except
if not Assigned(DBErrForm) then //dberrform连接错误时重新设置数据源的窗口
DBErrForm := TDBErrForm.Create(self);
DBErrForm.ShowModal;
DBErrForm.Free;
end;
end;
procedure Tfrmmain.FormShow(Sender: TObject);
begin
if not NewLogin then Application.Terminate;
end;
运行出错提示如下:
project inspection.exe raised exception class EAccessvialation with message 'access vialaion at address 005dbd1b in module 'inspection.exe'.Read of address FFFFFFFF'.