怎样写c++ builder6.0的ini配置文件呢????
我现在想让我编的这个小软件在其他电脑上运行,但因为链接了数据库,一运行它就提示未链接SQL数据,怎样可以通过写ini文件手动输入数据库ip或者一运行自动读取电脑的ip地址来解决这个问题??我试着写了一部分,但我不知道接下来怎么写,UserPS、UserName、ServerIP可以正确显示,但不知道怎么把它们串起来,现迫切希望各位大哥大姐能帮帮小弟。
我的Config.ini文件内容是:
//数据库配置
[Data]
//用户名密码是否加密
IsEncryption=0
//远程数据库配置
ServerIP=.
DataName=bianyaqi
UserName=sa
UserPS=123456
以下是我写的一部分ini程序:
//----获取运行程序的目录开始
Char dir[MAX_PATH];
Char *tfile = "/Config.ini";
//将程序现在目录所在输入此字符数组
::GetCurrentDirectory(MAX_PATH,dir);
//将现在目录加上指定文件名
strcat(dir,tfile);
//----获取运行程序的目录结束
TIniFile *ini=new TIniFile(dir);
AnsiString UserPS=ini->ReadString("Data","UserPS","默認值");
AnsiString UserName=ini->ReadString("Data","UserName","默認值");
AnsiString DataName=ini->ReadString("Data","DataName","默認值");
AnsiString ServerIP=ini->ReadString("Data","ServerIP","默認值");
delete ini;
ShowMessage(UserPS);
ShowMessage(UserName);
ShowMessage(ServerIP);//这里可以正确读取UserPS、UserName、ServerIP但接下来我不知道怎样做,帮帮忙各位!!
[解决办法]
你用什么连数据库呢?
如果是BDE的话,可以这么写:
TDatabase *db = new TDatabase(NULL);
db->LoginPrompt = false;
db->DatabaseName = szDbName;
db->Param->Clear();
db->Params->Add("user name=" + AnsiString(UserName));
db->Params->Add("password=" + AnsiString(Password));
db->Connected = true;
如果是ADO的话,可以写成
TADOConnection *db = new TADOConnection(NULL);
db->LoginPrompt = false;
db->ConnectionString = szConnStr;
db->Connected = true;
其中,szConnStr与具体的数据库种类有关,如果是Access数据库,可以写成
sprintf(szConnStr, "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;Password=%s", filename, password);
[解决办法]
本人不睬,仅供参考,
void __fastcall TDataMod::TelDataInit(void)
{
String connStr="",tempStr="";
int j=0;
TIniFile *ini;
try
{
ADOConn1->Close();
// connStr="Provider=SQLOLEDB.1;Password=xzsat;Persist Security Info=True;User ID=sa;Initial Catalog=daw;Data Source=XJQ-SAT";
ini=new TIniFile(".//SAS_Config.ini");
tempStr=ini->ReadString("dataBase","数据库类型","SQLSERVER");
ini->WriteString("dataBase","数据库类型",tempStr);
if(tempStr=="SQLSERVER") //连接SQL SERVER
{
connStr="Provider=SQLOLEDB.1;Persist Security Info=True;";
tempStr=ini->ReadString("dataBase","密码","xzsat");
ini->WriteString("dataBase","密码",tempStr);
connStr+="Password="+tempStr+";" ;
tempStr=ini->ReadString("dataBase","用户名","sa");
connStr+="User ID="+tempStr+";" ;
ini->WriteString("dataBase","用户名",tempStr);
tempStr=ini->ReadString("dataBase","数据库名称","daw");
connStr+="Initial Catalog="+tempStr+";" ;
ini->WriteString("dataBase","数据库名称",tempStr);
tempStr=ini->ReadString("dataBase","服务器名(IP)","XJQ-SAT");
connStr+="Data Source="+tempStr+" " ;
ini->WriteString("dataBase","服务器名(IP)",tempStr);
}
else //连接ACCESS
{
// Provider=Microsoft.Jet.OLEDB.4.0;Password=123;Data Source=E:\VehData.mdb;Persist Security Info=True
connStr="Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=True;";
tempStr=ini->ReadString("dataBase","密码","");
ini->WriteString("dataBase","密码",tempStr);
connStr+="Password="+tempStr+";" ;
//tempStr=ini->ReadString("dataBase","用户名","Admin");
//connStr+="User ID="+tempStr+";" ;
//ini->WriteString("dataBase","用户名",tempStr);
tempStr=ini->ReadString("dataBase","数据库名称","E:\VehData.mdb");
connStr+="Data Source="+tempStr+";" ;
ini->WriteString("dataBase","数据库名称",tempStr);
}
delete ini;
ADOConn1->ConnectionString=connStr;
ADOConn1->Open();
if(ADOConn1->Connected)
{
//Memo1->Lines->Add("数据库连接成功……");
TStringList* List=new TStringList;
tempStr="UserTable";
ADOConn1->GetTableNames(List,false);
for(j=0;j<List->Count;j++)
{
if(tempStr==(List->Strings[j]))
break;
}
if(j>=List->Count)
AddUserTable();
UserTable->Close();
UserTable->TableName=tempStr;
UserTable->Open();
UserTable->Last();
tempStr= FormatDateTime("'Veh_'yyyymm",Date());
for(j=0;j<List->Count;j++)
{
if(tempStr==(List->Strings[j]))
break;
}
if(j>=List->Count)
AddDataTable();
InsertTable->Close();
DisplayTable->Close();
InsertTable->TableName=tempStr;
DisplayTable->TableName=tempStr;
//InsertTable->Open();
//InsertTable->Last();
delete List;
DelDataTable();
try
{
String SqlStr="Select Top 1000 * from ";
SqlStr+=tempStr;
SqlStr+=" order by VEH_ID desc";
DataMod->InsertQuery->Close();
DataMod->InsertQuery->SQL->Clear();
DataMod->InsertQuery->SQL->Add(SqlStr);
DataMod->InsertQuery->ExecSQL();
DataMod->InsertQuery->Open();
}
catch(...)
{
}
}
}
catch(...)
{
// ShowMessage("远程数据库连接失败,\n请检查网络连接和服务器!");
}
}