网上找的代码,用来避免连服务器的代码修改,但不知怎么搞的,报getConnString函数无效,连不上数据库,本人严格按照下述方法做的,为什么不行啊?是不是代码有错?郁闷啊,盼高手指点.
文件名:DB.ini   
  内容:   
  [DB]   
  Server=服务器名或者ip   
  DataBase=数据库名   
  LogId=数据库用户名   
  LogPassword=密码   
  --------------------   
  '模块代码:   
  Option   Explicit   
  Private   Declare   Function   GetPrivateProfileString   Lib   "kernel32"   Alias   "GetPrivateProfileStringA"   (ByVal   lpApplicationName   As   String,   ByVal   lpKeyName   As   Any,     
  ByVal   lpDefault   As   String,   ByVal   lpReturnedString   As   String,   ByVal   nSize   As   Long,   ByVal   lpFileName   As   String)   As   Long   
    
  '自定义读取INI函數   
  Private   Function   getIni(ByVal   pfileName   As   String,   ByVal   psection   As   String,   ByVal   pkey   As   String)   As   String   
          Dim   x   As   Long,   Buff   As   String   *   128,   i%   
          x   =   GetPrivateProfileString(psection,   pkey,   "",   Buff,   128,   pfileName)   
          i   =   InStr(Buff,   Chr(0))   
          getIni   =   Trim(Left(Buff,   i   -   1))   
  End   Function   
    
  Public   Sub   getConnString()   
          On   Error   Resume   Next   
    
          Dim   dataBase   As   String   *   255   
          Dim   serverName   As   String   *   255   
          Dim   userID   As   String   *   255   
          Dim   PWD   As   String   *   255   
          Dim   filePath   As   String     
          filePaht=App.Path   &   "\DB.ini"   
    
          userID   =   getIni(filePath,   "DB",   "LogID")   
          PWD   =   getIni(filePath,   "DB",   "LogPassword")   
          dataBase   =   getIni(filePath,   "DB",   "DataBase")   
          ServerName=   getIni(filePath,   "DB",   "Server")       
        
          getConnString=   "Provider=SQLOLEDB.1;Persist   Security   Info=False;User   ID="   &   Trim(userID)   &   ";Password="   &   Trim(PWD)   &   ";Initial   Catalog="   &   Trim(DataBase)   &   ";Data   Source="   &   Trim(serverName)   
  End   Sub   
  '窗体代码(需要引用ADO):   
  Private   Sub   Command1_Click()   
          Dim   cn   As   New   ADODB.Connection   
          cn.ConnectionString=getConnString   
          cn.Open   
          ......   
  End   Sub   

解决方案 »

  1.   

    filePath=App.Path   &   "\DB.ini"  
      

  2.   

    Dim cn As New ADODB.Connection
    cn.ConnectionString="Provider=SQLOLEDB.1;Persist Security Info=False;User ID="& Trim(userID)&";Password="& Trim(PWD) &";Initial Catalog=" &   Trim(DataBase) & ";Data Source="& Trim(serverName)&""
    cn.Open
    .....
    OR:cn.ConnectionString="Driver={SQL Server};Server=" & ServerName & ";Database=DataBase;Trusted_Connection=yes"
    cn.Open
      

  3.   

    不行啊 老大
    那我怎么调用INI里的文件啊
     肯定要用过程调用啊 
      

  4.   

    Public Function getConnString()
       ......
    end function
      

  5.   

    Public Function getConnString() As String
       ...... 
    end function
      

  6.   

    可能是你的数据库连接有问题吧,2000和2005的连接是不同的,上面的是2000的连接字段,或者你把INI的Server=服务器名或者ip改为Server=. 再试试,再不行加我553774891
      

  7.   

    "provider=sqloledb;data source=?;uid=?;pwd=?;database=?"用这句连试试。我用着没问题。
      

  8.   

    andyinfrasys 是个高手啊 大家加他啊
     问题经过他的调试解决  代码 没有问题  filePaht=App.Path   &   "\DB.ini"  改成绝对路径就可以了