急急急,非常急(菜鸟得问题)请各位帮忙指点(在线等待) 各位,我用得是ado连接得数据库。现在上面要求我要把ado中得连接ip改成动态得,并且放在类似于.ini和.txt得文件中,这样在以后直接改它们中得内容就相当于改了adoconnectionstr中得ip,请问可以吗?要是有得话,请告诉我怎么作,我刚刚学delphi不久。谢谢各位了。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 正好的我程序是这么做的,可以看一下:这是一个初始化的窗口,用以得到相关的信息,数据库中的是ORACLE:procedure SetConnectionString(ServerName, UserName, Password: string);var SYSINI: TINIFile; tmpstr: String;begin SYSINI := TIniFile.Create(ExtractFilePath(Application.ExeName)+'DB.INI'); try with SYSINI do begin WriteString('Database', 'ServerName', ServerName); WriteString('Database', 'UserName', UserName); tmpstr := Encrypt(Password, ''); WriteString('Database', 'Password', tmpstr); end; finally SYSINI.Free; end;end;procedure TInitForm.XP_Button1Click(Sender: TObject);begin if (trim(ServerName.Text)='') or (trim(UserName.Text)='') then begin ShowMessage('你的信息没有填写完整,请确认后重写!'); exit; end; SetConnectionString(ServerName.Text, UserName.Text, Password.Text); InitForm.ModalResult:= IDOK;end;这是在程序启动时写的代码:function GetConnectionString: string;var SYSINI: TINIFile; ServerName, UserName, Password, tmpstr: string;begin SYSINI := TIniFile.Create(ExtractFilePath(Application.ExeName)+'DB.INI'); try ServerName := SYSINI.ReadString('Database', 'ServerName', ''); UserName := SYSINI.ReadString('Database', 'UserName', ''); tmpstr := SYSINI.ReadString('Database', 'Password', ''); Password := Decrypt(tmpstr, ''); Result := ''; Result := 'Provider=OraOLEDB.Oracle.1;Password=' + Password + ';Persist Security Info=True;User ID=' + UserName + ';Data Source='+ServerName+';Extended Properties=""'; finally SYSINI.Free; end;end;function ConnectStrNull:Boolean;var SYSINI: TINIFile; tmpstr:String;begin Result:= True; SYSINI := TIniFile.Create(ExtractFilePath(Application.ExeName)+'DB.INI'); try tmpstr:= SYSINI.ReadString('Database', 'ServerName', ''); if tmpstr ='' then exit; tmpstr:= SYSINI.ReadString('Database', 'UserName', ''); if tmpstr ='' then exit; Result:= False; finally SYSINI.Free; end;end;begin Application.Initialize; Application.Title := '**管理系统'; if ConnectStrNull then begin InitForm:= TInitForm.Create(Application); try if not InitForm.ShowModal = IDOK then exit; finally InitForm.Free; InitForm:= nil; end; end else begin Application.CreateForm(TDataModule1, DataModule1); DataModule1.ADOConnection1.Connected:= false; try DataModule1.ADOConnection1.ConnectionString:= GetConnectionString; DataModule1.ADOConnection1.Connected:= true; except MessageBox(Application.Handle,'数据库初始化设置错误,请与系统管理员联系!','错误',MB_OK OR MB_ICONSTOP); Exit; end; Application.CreateForm(TMainform, Mainform); Application.Run; end;end. form create时从ini文件中取出链接字符串动态给conn赋值。 向这种问题的数据库连接,一般都写向注册表里,当然写入(*.txt)或(*.ini)文件也可以,(但大型软件一般不写入(*.txt),(*.ini),容易丢失)我这有一个写入注册表的例子:(是我公司的一个项目):利用注册表进行数据库连接usesWindows,Messages,SysUtils,Variants,Classes,Graphics, Controls, Forms,Dialogs,registry;public{ Public declarations }procedure reg; //定义注册过程varForm1: TForm1;regi: tregistry; //定义tregistry的变量str: string; //定义string的变量procedure TForm1.FormCreate(Sender: TObject);beginreg; //在Tform1.formcreate(sender:tobject)中调用reg过程;end; procedure TForm1.FormShow(Sender: TObject);begintryadoconnection1.Connected:=false;adoconnection1.ConnectionString:=str;adoconnection1.LoginPrompt:=false;adoconnection1.Connected:=true;showmessage('数据库连接成功');exceptshowmessage('数据库连接未成功');end;end; //在TForm1.FormShow(Sender: TObject)中创建连接procedure tform1.reg;beginstr:='Provider=OraOLEDB.Oracle.1;Password=cylx;Persist Security Info=True;User ID=zljc;Data Source=cjp_data';regi:=tregistry.Create;regi.RootKey:=HKEY_CURRENT_USER;if(regi.KeyExists('software\yaojian')=false)thenbeginregi.CreateKey('software\yaojian');regi.OpenKey('software\yaojian',true);regi.WriteString('yao',str);endelsebeginregi.OpenKey('software\yaojian',true);str:=regi.ReadString('yao');end;end; 见鬼了,delphi2007调试程序乱跳。 关于RAVE报表打印后导致相关的DBGRID 不能滚动的问题 关于表类编写的问题!!! 菜鸟又提问:Timage的问题 为广大编程爱好者提供软件发布平台 没有源码?怎么安装组件? 如果不返回窗口句柄,如何得到DLL中FORM的句柄? 怎么截取字符串 关于在Delphi中使用MSXML4.0的问题 动态连接数据库问题? 请不要在csdn里面讨论关于外挂的任何问题!! 把非格式文本向格式文本转化,进而导入ORACLE的问题
var
SYSINI: TINIFile;
tmpstr: String;
begin
SYSINI := TIniFile.Create(ExtractFilePath(Application.ExeName)+'DB.INI');
try
with SYSINI do
begin
WriteString('Database', 'ServerName', ServerName);
WriteString('Database', 'UserName', UserName);
tmpstr := Encrypt(Password, '');
WriteString('Database', 'Password', tmpstr);
end;
finally
SYSINI.Free;
end;
end;procedure TInitForm.XP_Button1Click(Sender: TObject);
begin
if (trim(ServerName.Text)='') or (trim(UserName.Text)='') then
begin
ShowMessage('你的信息没有填写完整,请确认后重写!');
exit;
end;
SetConnectionString(ServerName.Text, UserName.Text, Password.Text);
InitForm.ModalResult:= IDOK;
end;
这是在程序启动时写的代码:
function GetConnectionString: string;
var
SYSINI: TINIFile;
ServerName, UserName, Password, tmpstr: string;
begin
SYSINI := TIniFile.Create(ExtractFilePath(Application.ExeName)+'DB.INI');
try
ServerName := SYSINI.ReadString('Database', 'ServerName', '');
UserName := SYSINI.ReadString('Database', 'UserName', '');
tmpstr := SYSINI.ReadString('Database', 'Password', '');
Password := Decrypt(tmpstr, '');
Result := '';
Result := 'Provider=OraOLEDB.Oracle.1;Password=' + Password + ';Persist Security Info=True;User ID=' + UserName + ';Data Source='+ServerName+';Extended Properties=""';
finally
SYSINI.Free;
end;
end;function ConnectStrNull:Boolean;
var
SYSINI: TINIFile;
tmpstr:String;
begin
Result:= True;
SYSINI := TIniFile.Create(ExtractFilePath(Application.ExeName)+'DB.INI');
try
tmpstr:= SYSINI.ReadString('Database', 'ServerName', '');
if tmpstr ='' then
exit;
tmpstr:= SYSINI.ReadString('Database', 'UserName', '');
if tmpstr ='' then
exit;
Result:= False;
finally
SYSINI.Free;
end;
end;begin
Application.Initialize;
Application.Title := '**管理系统'; if ConnectStrNull then
begin
InitForm:= TInitForm.Create(Application);
try
if not InitForm.ShowModal = IDOK then
exit;
finally
InitForm.Free;
InitForm:= nil;
end;
end
else
begin
Application.CreateForm(TDataModule1, DataModule1);
DataModule1.ADOConnection1.Connected:= false;
try
DataModule1.ADOConnection1.ConnectionString:= GetConnectionString;
DataModule1.ADOConnection1.Connected:= true;
except
MessageBox(Application.Handle,'数据库初始化设置错误,请与系统管理员联系!','错误',MB_OK OR MB_ICONSTOP);
Exit;
end;
Application.CreateForm(TMainform, Mainform);
Application.Run;
end;
end.
利用注册表进行数据库连接
uses
Windows,Messages,SysUtils,Variants,Classes,Graphics, Controls, Forms,Dialogs,registry;
public
{ Public declarations }
procedure reg; //定义注册过程
var
Form1: TForm1;
regi: tregistry; //定义tregistry的变量
str: string; //定义string的变量
procedure TForm1.FormCreate(Sender: TObject);
begin
reg; //在Tform1.formcreate(sender:tobject)中调用reg过程;
end;
procedure TForm1.FormShow(Sender: TObject);
begin
try
adoconnection1.Connected:=false;
adoconnection1.ConnectionString:=str;
adoconnection1.LoginPrompt:=false;
adoconnection1.Connected:=true;
showmessage('数据库连接成功');
except
showmessage('数据库连接未成功');
end;
end; //在TForm1.FormShow(Sender: TObject)中创建连接
procedure tform1.reg;
begin
str:='Provider=OraOLEDB.Oracle.1;Password=cylx;Persist Security Info=True;User ID=zljc;Data Source=cjp_data';
regi:=tregistry.Create;
regi.RootKey:=HKEY_CURRENT_USER;
if(regi.KeyExists('software\yaojian')=false)then
begin
regi.CreateKey('software\yaojian');
regi.OpenKey('software\yaojian',true);
regi.WriteString('yao',str);
end
else
begin
regi.OpenKey('software\yaojian',true);
str:=regi.ReadString('yao');
end;
end;