8 个解决方案
#1
其实一样的,也可以用文件存储的
Path = ExtractFileDir(Application->ExeName);
Config = new TIniFile(Path + "\\Config.ini");
SQLLinkString = Config->ReadString("DateBase","LinkString","");
delete Config;
ADOConnection1->ConnectionString = SQLLinkString ;
#2
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请检查网络连接和服务器!");
}
#3
这是我的ini文件代码:
Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=rxfh
下面是连接数据库的代码:
String Path,SQLLinkString;
TIniFile *Config;
try
{
Path = ExtractFileDir(Application->ExeName);
Config = new TIniFile(Path + "\\local.ini");
SQLLinkString = Config->ReadString("DateBase","LinkString","");
delete Config;
ADOConnection1->ConnectionString = SQLLinkString;
ADOConnection1->Open();
}
catch(...)
{
Application->MessageBox("数据库连接失败!","运行错误",MB_ICONERROR|MB_OK);
}
运行后连不上数据库,调试时看字符串SQLLinkString为空,是不是SQLLinkString = Config->ReadString("DateBase","LinkString","");有错?麻烦帮忙看一下,谢谢!
Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=rxfh
下面是连接数据库的代码:
String Path,SQLLinkString;
TIniFile *Config;
try
{
Path = ExtractFileDir(Application->ExeName);
Config = new TIniFile(Path + "\\local.ini");
SQLLinkString = Config->ReadString("DateBase","LinkString","");
delete Config;
ADOConnection1->ConnectionString = SQLLinkString;
ADOConnection1->Open();
}
catch(...)
{
Application->MessageBox("数据库连接失败!","运行错误",MB_ICONERROR|MB_OK);
}
运行后连不上数据库,调试时看字符串SQLLinkString为空,是不是SQLLinkString = Config->ReadString("DateBase","LinkString","");有错?麻烦帮忙看一下,谢谢!
#4
[DateBase]
LinkString=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=rxfh
ini文件改成这样
#5
我试了一下,现在字符串SQLLinkString对了,但连接时仍提示错误“未发现数据源名称并且未指定默认驱动程序”。请问是何原因?谢谢!
#6
你这个连接字符串是不是有问题???你在编程的时候单击ADOConnection1控件--》在左边的对象的属性列表里面点ConnectionString 然后生成一个能用的连接串,复制过来就行了。
#7
我就是复制的这个字符串,测试连接通过了的.
#8
那你就检查读出来的串和你设置的一样不一样,一样就不是读取的问题,不一样就看是读的时候读错了,检查是不是空格影响了字符串
#1
其实一样的,也可以用文件存储的
Path = ExtractFileDir(Application->ExeName);
Config = new TIniFile(Path + "\\Config.ini");
SQLLinkString = Config->ReadString("DateBase","LinkString","");
delete Config;
ADOConnection1->ConnectionString = SQLLinkString ;
#2
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请检查网络连接和服务器!");
}
#3
这是我的ini文件代码:
Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=rxfh
下面是连接数据库的代码:
String Path,SQLLinkString;
TIniFile *Config;
try
{
Path = ExtractFileDir(Application->ExeName);
Config = new TIniFile(Path + "\\local.ini");
SQLLinkString = Config->ReadString("DateBase","LinkString","");
delete Config;
ADOConnection1->ConnectionString = SQLLinkString;
ADOConnection1->Open();
}
catch(...)
{
Application->MessageBox("数据库连接失败!","运行错误",MB_ICONERROR|MB_OK);
}
运行后连不上数据库,调试时看字符串SQLLinkString为空,是不是SQLLinkString = Config->ReadString("DateBase","LinkString","");有错?麻烦帮忙看一下,谢谢!
Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=rxfh
下面是连接数据库的代码:
String Path,SQLLinkString;
TIniFile *Config;
try
{
Path = ExtractFileDir(Application->ExeName);
Config = new TIniFile(Path + "\\local.ini");
SQLLinkString = Config->ReadString("DateBase","LinkString","");
delete Config;
ADOConnection1->ConnectionString = SQLLinkString;
ADOConnection1->Open();
}
catch(...)
{
Application->MessageBox("数据库连接失败!","运行错误",MB_ICONERROR|MB_OK);
}
运行后连不上数据库,调试时看字符串SQLLinkString为空,是不是SQLLinkString = Config->ReadString("DateBase","LinkString","");有错?麻烦帮忙看一下,谢谢!
#4
[DateBase]
LinkString=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=rxfh
ini文件改成这样
#5
我试了一下,现在字符串SQLLinkString对了,但连接时仍提示错误“未发现数据源名称并且未指定默认驱动程序”。请问是何原因?谢谢!
#6
你这个连接字符串是不是有问题???你在编程的时候单击ADOConnection1控件--》在左边的对象的属性列表里面点ConnectionString 然后生成一个能用的连接串,复制过来就行了。
#7
我就是复制的这个字符串,测试连接通过了的.
#8
那你就检查读出来的串和你设置的一样不一样,一样就不是读取的问题,不一样就看是读的时候读错了,检查是不是空格影响了字符串