function Tfrm_main.getConnStr:string;
var
serverini:tinifile;
vProvider:string; //驱动类型
vPassword:string; //密码
vPSI:string; //是否使用持续安全验证
vUserID:string; //用户ID
vInitialCatalog:string;//数据库名称
vDataSource:string; //服务器名称
begin
if FileExists(ExtractFilePath(Paramstr(0))+'\ConnectServer.ini') then
begin
ServerIni:=tIniFile.Create(ExtractFilePath(Paramstr(0))+'\ConnectServer.ini');
vProvider:=ServerIni.ReadString('Paramer','Provider','Hpperfect');
vPassword:=ServerIni.ReadString('Paramer','Password','123');
vPSI:=ServerIni.ReadString('Paramer','Persist Security Info','True');
vUserID:=ServerIni.ReadString('Paramer','User ID','sa');
vInitialCatalog:=ServerIni.ReadString('Paramer','Initial Catalog','DyeTGT');
vDataSource:=ServerIni.ReadString('Paramer','Data Source','jm-sc');
result:= 'Provider=SQLOLEDB.1;'+'Password=' + vpassword + ';Persist Security Info=True;User ID=' + vuserid + ';Initial Catalog=' + vInitialCatalog + ';Data Source=' + vDataSource;
ServerIni.Free;
end
在程序中,获取adoconnection.connectstring的结果变成了,后面多了很多东西(下划线的不是我想要的),搞到连接不上数据库,究竟我的函数哪里出错了?Provider=SQLOLEDB.1;Password=123;Persist Security Info=True;User ID=sa;Initial Catalog=DyeTGT;Data Source=tgtWORK;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=TGTWORK;Use Encryption for Data=False;Tag with column collation when possible=False
var
serverini:tinifile;
vProvider:string; //驱动类型
vPassword:string; //密码
vPSI:string; //是否使用持续安全验证
vUserID:string; //用户ID
vInitialCatalog:string;//数据库名称
vDataSource:string; //服务器名称
begin
if FileExists(ExtractFilePath(Paramstr(0))+'\ConnectServer.ini') then
begin
ServerIni:=tIniFile.Create(ExtractFilePath(Paramstr(0))+'\ConnectServer.ini');
vProvider:=ServerIni.ReadString('Paramer','Provider','Hpperfect');
vPassword:=ServerIni.ReadString('Paramer','Password','123');
vPSI:=ServerIni.ReadString('Paramer','Persist Security Info','True');
vUserID:=ServerIni.ReadString('Paramer','User ID','sa');
vInitialCatalog:=ServerIni.ReadString('Paramer','Initial Catalog','DyeTGT');
vDataSource:=ServerIni.ReadString('Paramer','Data Source','jm-sc');
result:= 'Provider=SQLOLEDB.1;'+'Password=' + vpassword + ';Persist Security Info=True;User ID=' + vuserid + ';Initial Catalog=' + vInitialCatalog + ';Data Source=' + vDataSource;
ServerIni.Free;
end
在程序中,获取adoconnection.connectstring的结果变成了,后面多了很多东西(下划线的不是我想要的),搞到连接不上数据库,究竟我的函数哪里出错了?Provider=SQLOLEDB.1;Password=123;Persist Security Info=True;User ID=sa;Initial Catalog=DyeTGT;Data Source=tgtWORK;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=TGTWORK;Use Encryption for Data=False;Tag with column collation when possible=False
Provider=SQLOLEDB.1
Password=123
Persist Security Info=True
User ID=sa
Initial Catalog=DyeTGT
Data Source=tgtWORK
DataServer=tgtWORK
Provider=SQLOLEDB.1
Password=123
Persist Security Info=True
User ID=sa
Initial Catalog=DyeTGT
Data Source=tgtWORK
返回的adoconnection.connectString是带下划线的也没有错
我的Ini文件:
[Paramer]
Provider:=dddd;
Password:=123;
Persist Security Info:=true;
User ID:=sa;
Initial Catalog:=DyeTGT';
Data Source:=jm-sc;测试代码:
procedure TForm1.Button5Click(Sender: TObject);
var
serverini:tinifile;
vProvider:string; //驱动类型
vPassword:string; //密码
vPSI:string; //是否使用持续安全验证
vUserID:string; //用户ID
vInitialCatalog:string;//数据库名称
vDataSource:string; //服务器名称
begin
if FileExists('c:\ConnectServer.ini') then
begin
ServerIni:=tIniFile.Create(ExtractFilePath(Paramstr(0))+'\ConnectServer.ini');
vProvider:=ServerIni.ReadString('Paramer','Provider','Hpperfect');
vPassword:=ServerIni.ReadString('Paramer','Password','123');
vPSI:=ServerIni.ReadString('Paramer','Persist Security Info','True');
vUserID:=ServerIni.ReadString('Paramer','User ID','sa');
vInitialCatalog:=ServerIni.ReadString('Paramer','Initial Catalog','DyeTGT');
vDataSource:=ServerIni.ReadString('Paramer','Data Source','jm-sc');
showmessage('Provider=SQLOLEDB.1;'+'Password=' + vpassword + ';Persist Security Info=True;User ID=' + vuserid + ';Initial Catalog=' + vInitialCatalog + ';Data Source=' + vDataSource);
ServerIni.Free;
end
end;
可以删掉现在的adoconnection,然后新加一个同名的adoconnection,再试试
然后你再showmessage(adoconnection.connectstring)看看是多少。
Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=TGTWORK;Use Encryption for Data=False;Tag with column collation when possible=False
然后,再看你的adoconnection是怎么调用的,问题应该出在这。
你可以这样调试:
1.先写一个Demo验证getConnstr的正确性,看看能否得到正确的结果。先排除函数的问题。
2.然后查看你调用的位置,单步跟踪看看。
找到问题所在应该不难。
我碰到过arr1: array[1..5],最后访问了arr1[6]的情况,就得出这样的结果。
或者某个变量没有初始化
你这样改下,你就能看到了,是不是读你ConnectServer.ini中的内容了。
begin
if FileExists('c:\ConnectServer.ini') then
begin
ServerIni:=tIniFile.Create(ExtractFilePath(Paramstr(0))+'\ConnectServer.ini');
vProvider:=ServerIni.ReadString('Paramer','Provider','0');
vPassword:=ServerIni.ReadString('Paramer','Password','0');
vPSI:=ServerIni.ReadString('Paramer','Persist Security Info','0');
vUserID:=ServerIni.ReadString('Paramer','User ID','0');
vInitialCatalog:=ServerIni.ReadString('Paramer','Initial Catalog','0');
vDataSource:=ServerIni.ReadString('Paramer','Data Source','0');
showmessage('Provider=SQLOLEDB.1;'+'Password=' + vpassword + ';Persist Security Info=True;User ID=' + vuserid + ';Initial Catalog=' + vInitialCatalog + ';Data Source=' + vDataSource);
ServerIni.Free;
end
end;