本人做的一个三层数据库,以下是实现从DB.ini配置文件的读取参数和一个判断数据库是否连接的函数
Database: TDatabase;
.....
//读取配置文件信息
procedure ReadFromIni(var aServerName, aDatabaseName, aUserName, aPassword: string);
var
IniFile: TIniFile;
FileName: string;
begin
FileName := ExtractFilePath(application.ExeName) + '\DB.ini';
IniFile := TIniFile.Create(FileName);
aServerName := IniFile.ReadString('DB', 'ServerName', '');
aDatabaseName := IniFile.ReadString('DB', 'DatabaseName', '');
aUserName := IniFile.ReadString('DB', 'UserName', '');
aPassword := IniFile.ReadString('DB', 'Password', '');
IniFile.Free;
end;
..
//判断能否连接上数据库的函数
function TfrmMain.DBConnect(aServerName, aDatabaseName, aUserName, aPassword: string): boolean;
begin
Result := True;
Database.connected := False;
Database.DriverName := 'SQL server'; //驱动名称
Database.Params.Values['Server Name'] := aServerName; //服务器名
Database.Params.Values['Database Name'] := aDatabaseName; //数据库名
Database.Params.Values['User Name'] := aUserName;//登录数据库用户名
Database.Params.Values['Password'] := aPassword; //相应数据库用户的密码
try
Database.Connected := True;
except
result := False;
end;
end;
....
DB.ini内容如下
[DB]
ServerName=king
DatabaseName=wzgl
UserName=sa
Password=
...另一段代码
//写入ini,并测试是否连接上
procedure TfrmDBConnect.btnOKClick(Sender: TObject);
begin
writeToIni(edtServerName.Text, edtDatabaseName.Text, edtUserName.Text, edtPassword.Text);
if frmMain.DBConnect(edtServerName.Text, edtDatabaseName.Text, edtUserName.Text, edtPassword.Text) then
begin
Application.MessageBox('连接成功', '提示', mb_iconInformation + mb_defbutton1);
Connected := True;
Close;
end
else
begin
Application.MessageBox('连接失败', '提示', mb_iconInformation + mb_defbutton1);
end;
end;为什么总是提示'连接失败'?
Database: TDatabase;
.....
//读取配置文件信息
procedure ReadFromIni(var aServerName, aDatabaseName, aUserName, aPassword: string);
var
IniFile: TIniFile;
FileName: string;
begin
FileName := ExtractFilePath(application.ExeName) + '\DB.ini';
IniFile := TIniFile.Create(FileName);
aServerName := IniFile.ReadString('DB', 'ServerName', '');
aDatabaseName := IniFile.ReadString('DB', 'DatabaseName', '');
aUserName := IniFile.ReadString('DB', 'UserName', '');
aPassword := IniFile.ReadString('DB', 'Password', '');
IniFile.Free;
end;
..
//判断能否连接上数据库的函数
function TfrmMain.DBConnect(aServerName, aDatabaseName, aUserName, aPassword: string): boolean;
begin
Result := True;
Database.connected := False;
Database.DriverName := 'SQL server'; //驱动名称
Database.Params.Values['Server Name'] := aServerName; //服务器名
Database.Params.Values['Database Name'] := aDatabaseName; //数据库名
Database.Params.Values['User Name'] := aUserName;//登录数据库用户名
Database.Params.Values['Password'] := aPassword; //相应数据库用户的密码
try
Database.Connected := True;
except
result := False;
end;
end;
....
DB.ini内容如下
[DB]
ServerName=king
DatabaseName=wzgl
UserName=sa
Password=
...另一段代码
//写入ini,并测试是否连接上
procedure TfrmDBConnect.btnOKClick(Sender: TObject);
begin
writeToIni(edtServerName.Text, edtDatabaseName.Text, edtUserName.Text, edtPassword.Text);
if frmMain.DBConnect(edtServerName.Text, edtDatabaseName.Text, edtUserName.Text, edtPassword.Text) then
begin
Application.MessageBox('连接成功', '提示', mb_iconInformation + mb_defbutton1);
Connected := True;
Close;
end
else
begin
Application.MessageBox('连接失败', '提示', mb_iconInformation + mb_defbutton1);
end;
end;为什么总是提示'连接失败'?
procedure WriteToIni(const aServerName, aDatabaseName, aUserName, aPassword: string);
var
IniFile: TIniFile;
FileName: string;
begin
FileName := ExtractFilePath(application.ExeName) + '\DB.ini';
IniFile := TIniFile.Create(FileName);
IniFile.WriteString('DB', 'ServerName', aServerName);
IniFile.WriteString('DB', 'DatabaseName', aDatabaseName);
IniFile.WriteString('DB', 'UserName', aUserName);
IniFile.WriteString('DB', 'Password', aPassword);
IniFile.Free;
end;
但我这段语句却能实现向DB.ini写入信息?
unit Unit2;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls,inifiles;type
TForm2 = class(TForm)
Timer1: TTimer;
Label1: TLabel;
procedure FormCreate(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form2: TForm2;
pos:integer;
speed:integer;
ox,oy:integer;
Hascall:boolean;
implementation{$R *.DFM}
uses unit1;
procedure TForm2.FormCreate(Sender: TObject);
var
textinifile:tinifile;
s:set of Tfontstyle;
begin
form2.WindowState:=wsMaximized;
form2.BorderStyle:=bsNone; Textinifile:=Tinifile.Create('Mysaver.ini');
if textinifile<>nil then
try
with textinifile do
begin
form2.Color:=ReadInteger('ScrClor','BColor',clBlack);
form2.Label1.Caption:=ReadString('ScrSaver','text','delphi');
pos:=ReadInteger('ScrSaver','position',0);
speed:=ReadInteger('ScrSaver','speed',0); form2.Label1.Font.Charset:=Readinteger('ScrFont','Charset',GB2312_CHARSET);
form2.Label1.Font.Name:=Readstring('ScrFont','Name','宋体');
form2.Label1.Font.Color:=readinteger('ScrFont','Color',clRed);
form2.Label1.Font.Size:=ReadInteger('ScrFont','Size',13);
form2.Label1.Font.Height:=Readinteger('ScrFont','Height',11);
s:=[];
if readinteger('ScrFont','StyleB',0)=1 then s:=S+[fsBold];
if readinteger('ScrFont','StyleI',0)=1 then s:=S+[fsItalic];
if readinteger('ScrFont','StyleU',0)=1 then s:=S+[fsUnderline];
if readinteger('ScrFont','StyleS',0)=1 then s:=S+[fsStrikeOut];
form2.Label1.Font.Style:=s;
Free;
end;
except
textinifile.free;
end;
form2.Timer1.interval:=50-4*speed;
randomize;
if pos=0 then form2.Label1.Top:=round(screen.Height/2)
else form2.Label1.top:=random(screen.Height-50);
cursor:=crNone;
form2.Label1.cursor:=crNone;
end;procedure TForm2.Timer1Timer(Sender: TObject);
begin
randomize;
Form2.Label1.Left:=label1.Left-3-round(speed/2);
if label1.Left+label1.width<0 then
begin
label1.top:=random(screen.height-50);
label1.Left:=screen.width;
end;
end;procedure TForm2.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if not hascall then
begin
ox:=x;
oy:=y;
hascall:=true;
end;
if (x<>ox)or(y<>oy) then
begin
close;
end;
end;end.
memo1.Lines.LoadFromFile('d:\\\\\\\f\\\\\\\\\f1.txt');
和
memo1.Lines.LoadFromFile('d:\f\f1.txt');
效果是一样的。
另外给Database.databasename赋值,一个在BDE别名中没有的值
Database.DriverName := 'SQL server'; //驱动名称
Database.Params.Values['Server Name'] := aServerName; //服务器名
Database.Params.Values['Database Name'] := aDatabaseName; //数据库名
Database.Params.Values['User Name'] := aUserName;//登录数据库用户名
Database.Params.Values['Password'] := aPassword; //相应数据库用户的密码
这些地方有问题,其他的没什么问题!
你对database控件所写的代码实际是对它的一个配置,要通过配置好的database控件连接到数据库,必须要通过建立别名才能实现
不是要建别名,而是
1.database.databasename不能为空,
2.如果database.databasename的值在bde的别名中有,其他的参数,系统会自动调bde中的参数
请看帮助
Use DatabaseName to specify the name of the database to use with a database component. If DatabaseName is the same as an existing Borland Database Engine (BDE) alias, then the AliasName and DriverName properties need not be set. If DatabaseName does not match an existing BDE alias, then either the application must also supply a valid alias in the AliasName property in addition to the DatabaseName, or it must supply the DriverName and Params properties.
Database.DriverName := 'SQL server'; //驱动名称
Database.Params.Values['Server Name'] := aServerName; //服务器名
Database.Params.Values['Database Name'] := aDatabaseName; //数据库名
Database.Params.Values['User Name'] := aUserName;//登录数据库用户名
Database.Params.Values['Password'] := aPassword; //相应数据库用户的密码
而是在DataBase组件中,直接设为Alias Name为一个已存在的BDE别名(ODBC),
Parameter Overries:
user name=sa
password
这样,其Connected属性可以设为True;
或者去掉上面语句中的
Database.DriverName := 'SQL server'; //驱动名称
这样编译出来的程序就可以通过,这是为什么?