ADO用文件连接数据库
我以前用BDE连接SQL Server数据库,现在用ADO,觉得很方便。但有一个问题:我以前将数据库配置文件存在一个ini文件中(主要是为了便于修改数据库服务器地址),然后用Database-》Params-》LoadFromFile打开文件。但ADO连接中没有该设置,请问如何实现(我作的程序需要开机即连接数据库运行,配置只能通过文件修改,不能通过编辑控件修改)?非常感谢!
[解决办法]
其实一样的,也可以用文件存储的
Path = ExtractFileDir(Application->ExeName);Config = new TIniFile(Path + "\\Config.ini");SQLLinkString = Config->ReadString("DateBase","LinkString","");delete Config;ADOConnection1->ConnectionString = SQLLinkString ;
[解决办法]
String connStr="",tempStr=""; 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("数据库连接成功……"); tempStr= FormatDateTime("'HSPEED_'yyyymm",Date()); TStringList* List=new TStringList; int j=0; ADOConn1->GetTableNames(List,false); 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(); DelDataTable(); tempStr="UserTable"; 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(); delete List; } } catch(...) { // ShowMessage("远程数据库连接失败,\n请检查网络连接和服务器!"); }
[解决办法]
[DateBase]LinkString=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=rxfh