procedure Tt_main.FormShow(Sender: TObject);
var
ini: TIniFile;
server,database,user,password: String;
begin
//设置窗体界面,即只显示窗体背景图片效果还是在窗体中嵌入一个子窗体
IntercontrolExecute(Sender);
//调用初始化窗口
Application.CreateForm(Tf_ini,f_ini);
f_ini.ShowModal;
f_ini.Free;
ini := nil;
//配置信息是否存在
if Not FileExists(ExtractFilePath(Application.ExeName)+'Login.ini') then
begin
//调用登录服务器窗口,登录服务器并记录登录信息
Application.CreateForm(Tf_loginserver,f_loginserver);
f_loginserver.ShowModal;
f_loginserver.Free;
end
else //读取配置信息
begin
Try
ini := TIniFile.Create(ExtractFilePath(Application.ExeName)+'Login.ini');
server := ini.ReadString('配置信息','服务器','');
database := ini.ReadString('配置信息','数据库','');
user := ini.ReadString('配置信息','用户名','');
password := ini.ReadString('配置信息','密码','');
Try
t_data.Connection1.ConnectionString := 'Provider=SQLOLEDB.1;Password='+Trim(Password)+';Persist Security Info=True;User ID='+Trim(User)+';Initial Catalog='+Trim(Database)+';Data Source='+Trim(Server);
t_data.Connection1.Open;
Except
if Application.MessageBox('数据库连接失败,是否重新连接数据库?','提示',mb_yesno) = ID_Yes then
begin
Application.CreateForm(Tf_loginserver,f_loginserver);
f_loginserver.ShowModal;
f_loginserver.Free;
end;
end;
Finally
ini.Free;
end;
end;
Login.Execute;
StatusBar1.Panels[Sta_Date].Text := FormatDateTime('yyyy-mm-dd',Now);
//加载AVI动画
if FileExists(ExtractFilePath(Application.ExeName)+'AVI\store.avi') then
begin
Animate.FileName := ExtractFilePath(Application.ExeName)+'AVI\store.avi';
Animate.Active := True;
end;
end;
以下是f_Loginserver 单元的主要源码
var
f_Loginserver: Tf_Loginserver;
Const
server = '服务器';
database = '数据库';
user = '用户名';
pass = '口令';
implementation
uses ComObj,data;
{$R *.dfm}
procedure Tf_Loginserver.CancelClick(Sender: TObject);
begin
inherited;
Close;
end;
procedure Tf_Loginserver.FormCreate(Sender: TObject);
var
i: Integer;
server,sql: Variant;
begin
inherited;
Try //列举局域网中的SQL服务器
Server := CreateOleObject('SQLDMO.Application');
sql := server.ListAvailableSQLServers;
For i := 1 to sql.Count-1 do
if Trim(sql.Item(i))<>'' then
ServerName.Items.Add(Trim(sql.Item(i)));
ServerName.ItemIndex := 0;
Finally
server := Null;
sql := null;
End;
end;
procedure Tf_Loginserver.ServernameKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
inherited;
if Key = VK_Return then
FindNext(True);
end;
//处理登录按钮的单击事件
procedure Tf_Loginserver.LoginClick(Sender: TObject);
var
Ini: TIniFile;
begin
Ini := Nil;
inherited;
Try
Try
t_Data.Connection1.Close;
t_data.Connection1.ConnectionString := 'Provider=SQLOLEDB.1;Password='+Trim(Password.Text)+';Persist Security Info=True;User ID='+Trim(Username.Text)+';Initial Catalog= Master'+';Data Source='+Trim(Servername.Text);
t_data.Connection1.Open;
With t_Data.Query1 do
begin
Close;
SQL.Clear;
SQL.Add('select * from sysdatabases where name = :name');
Parameters.ParamByName('Name').Value := DatabaseName.Text;
Open;
end;
if t_Data.Query1.RecordCount<1 then
begin
if Application.MessageBox(Pchar(Servername.Text+'服务器中没有安装程序所需的数据库,是否在该服务器中安装数据库?'),'提示',mb_yesno)= ID_Yes then
begin
//判断数据库文件是否在指定的路径下
if FileExists(ExtractFilePath(Application.ExeName)+'database\'+ 'db_bpglxt_Data.MDF')and
FileExists(ExtractFilePath(Application.ExeName)+'database\'+ 'db_bpglxt_Log.LDF')then
begin
Try
With t_Data.Query1 do
begin
Close;
SQL.Clear;
SQL.Add(' Exec sp_attach_db @dbname = :a , @filename1 = :b ,@filename2 = :c' );
Parameters.ParamByName('a').Value := DatabaseName.Text;
Parameters.ParamByName('b').Value := ExtractFilePath(Application.ExeName)+'database\'+ 'db_bpglxt_Data.MDF';
Parameters.ParamByName('c').Value := ExtractFilePath(Application.ExeName)+'database\'+ 'db_bpglxt_Log.LDF';
ExecSQL;
end;
Application.MessageBox('数据库安装成功.','提示',64);
Except
Application.MessageBox('数据库安装失败.','提示',64);
Exit;
end;
end
else
if Dialog1.Execute then
if Dialog1.Files.Count>1 then
begin
Try
With t_Data.Query1 do
begin
Close;
SQL.Clear;
SQL.Add(' Exec sp_attach_db @dbname = :a , @filename1 = :b ,@filename2 = :c' );
Parameters.ParamByName('a').Value := DatabaseName.Text;
Parameters.ParamByName('b').Value := Dialog1.Files[0];
Parameters.ParamByName('c').Value := Dialog1.Files[1];
ExecSQL;
end;
Application.MessageBox('数据库安装成功.','提示',64);
Except
Application.MessageBox('数据库安装失败.','提示',64);
Exit;
end;
end
else
begin
Application.MessageBox('请至少选择2个文件.','提示',64);
Exit;
end;
end;
end;
t_Data.Connection1.Close;
t_data.Connection1.ConnectionString := 'Provider=SQLOLEDB.1;Password='+Trim(Password.Text)+';Persist Security Info=True;User ID='+Trim(Username.Text)+';Initial Catalog='+Trim(Databasename.Text)+';Data Source='+Trim(Servername.Text);
t_data.Connection1.Open;
//验证数据库是否合法
with t_data.Query1 do
begin
Close;
SQL.Clear;
SQL.Add('select * from sysobjects where xtype = ''u'' and name = ''tb_storeserviceID'' ');
open;
end;
if t_data.Query1.RecordCount <1 then //数据库同名,但不是本系统使用的数据库
begin
Application.MessageBox('该数据库已经存在,请重新设置数据库名称.','提示',64);
DatabaseName.SelectAll;
Databasename.SetFocus;
Exit;
end;
Except
Application.MessageBox('数据库连接失败.','提示',64);
Exit;
end;
//利用Ini文件记录数据库配置信息
Ini := TIniFile.Create(ExtractFilePath(Application.ExeName)+'Login.ini');
Ini.WriteString('配置信息',server,Trim(Servername.Text));
Ini.WriteString('配置信息',database,Trim(Databasename.Text));
Ini.WriteString('配置信息',user,Trim(Username.Text));
Ini.WriteString('配置信息',pass,Trim(Password.Text));
Application.MessageBox('数据库连接成功.','提示',64);
Close;
Finally
Ini.Free;
End;
end;
摘自《物资管理系统》