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

解决方案 »

  1.   

    [Paramer]
    Provider=SQLOLEDB.1
    Password=123
    Persist Security Info=True
    User ID=sa
    Initial Catalog=DyeTGT
    Data Source=tgtWORK
    DataServer=tgtWORK
      

  2.   

    [Paramer] 
    Provider=SQLOLEDB.1 
    Password=123 
    Persist Security Info=True 
    User ID=sa 
    Initial Catalog=DyeTGT 
    Data Source=tgtWORK 
      

  3.   

    函数感觉没有什么错
    返回的adoconnection.connectString是带下划线的也没有错
      

  4.   

    我试了,没有问题,你看看其它部分的代码吧
    我的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;
      

  5.   

    不想要有下划线的部分?  你对adoconnection做了些设置什么的吧
    可以删掉现在的adoconnection,然后新加一个同名的adoconnection,再试试
      

  6.   

    adoconnection.connectstring := getConnStr;
    然后你再showmessage(adoconnection.connectstring)看看是多少。
      

  7.   

    还是一样,不明白为什么多了这些出来,搞到连接数据库失败了
    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
      

  8.   

    设断点跟踪,先看一下函数得出来的字符串是什么。
    然后,再看你的adoconnection是怎么调用的,问题应该出在这。
      

  9.   

    慢慢来,我好几次调试ini快崩溃的时候,才发现读取的ini文件和我看见的不是一个,呵呵
      

  10.   

    楼主的这个函数getConnStr没有问题,关键是你调用的时候是如何调用的?
    你可以这样调试:
    1.先写一个Demo验证getConnstr的正确性,看看能否得到正确的结果。先排除函数的问题。
    2.然后查看你调用的位置,单步跟踪看看。
    找到问题所在应该不难。
      

  11.   

    串变量无故多出内容,以我的经验来看是因为变量的地址出了问题:在某个操作中意外修改了串变量的长度?
    我碰到过arr1: array[1..5],最后访问了arr1[6]的情况,就得出这样的结果。
    或者某个变量没有初始化
      

  12.   

    你的测试文件好像又问题,你根本就没读ConnectServer.ini中的内容。
    你这样改下,你就能看到了,是不是读你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;