ADOQUERY 如何用代码修改服务器地址
就是在ADOquery控件里有个connectionstring 选项里面 的使用连结字符串 选项里有个输入 服务器名称的选项,请问如何在程序里用代码对其进行修改
[解决办法]
String iniFile;
iniFile = ExtractFilePath(Application->ExeName)+"\\dbconfig.ini";
try
{
if (!FileExists(iniFile) )
{
ShowMessage(iniFile+"文件不存在!");
return -1;
}
TIniFile * ini;
ini = new TIniFile(iniFile);
VP_AliasName=ini->ReadString( "PARA","AliasName","");
VP_DBName=ini->ReadString( "PARA","DBName","");
VP_UserID=ini->ReadString( "PARA","USER","").Trim();
VP_PassWord=ini->ReadString( "PARA","PASSWORD","");
VP_ServerName=ini->ReadString("PARA","DBSERVERIP","");
delete ini;
----------------
ADOConnection1->ConnectionString = String().sprintf
"Provider=SQLOLEDB.1;"
"Persist Security Info=True;"
"Data Source=%s;"
"User ID=%s;"
"Password=%s;"
"Initial Catalog=%s",
HostName,UserID ,Password,DBName
)
;
[解决办法]
#include <inifiles.hpp>void __fastcall TForm1::Button1Click(TObject *Sender){ String strIniFile = ChangeFileExt(ParamStr(0), ".ini"); String strDbSvr, strDbName, strDbUsr, strDbPwd; TIniFile *ini = new TIniFile(strIniFile); strDbSvr = ini->ReadString("Database", "DbSvr", "192.168.1.1"); strDbName = ini->ReadString("Database", "DbName", "数据库名"); strDbUsr = ini->ReadString("Database", "DbUsr", "sa"); strDbPwd = ini->ReadString("Database", "DbPwd", "你的密码"); delete ini; ADOQuery->ConnectionString = String().sprintf( TEXT("Provider=SQLOLEDB.1;") TEXT("Persist Security Info=True;") TEXT("Data Source=%s;") TEXT("User ID=%s;") TEXT("Password=%s;") TEXT("Initial Catalog=%s), strDbSvr, strDbUsr, strDbPwd, strDbName);}