一个简单的方法,采用赋值的方法,首先建立一个文本文件,然后读给adoconnect的connectionstring,connected:=true,以后修改文本文件就行了,当然文本文件的内容可以使用连接字符串编辑器的build生成

解决方案 »

  1.   

    你可以根据不同的数据库参数动态生成TADOConnect.ConnectionString.我是用INI来完成的,你也可以用UDL文件
    function GetConnectionString: string;
    var
      SYSINI: TINIFile;
      ServerName, UserName, Password, InitDB: string;
      tmpstr: string;
    begin
      SYSINI := TIniFile.Create('DB.INI');
      try
        ServerName := SYSINI.ReadString('Database', 'ServerName', '');
        UserName := SYSINI.ReadString('Database', 'UserName', '');
        InitDB := SYSINI.ReadString('Database', 'InitDB', '');
        tmpstr := SYSINI.ReadString('Database', 'Password', '');
        Password := Decrypt(tmpstr, Key);
        Result := '';
        Result := 'Provider=SQLOLEDB.1;Password=' + Password + ';Persist Security Info=True;User ID=' + UserName + ';Initial Catalog=' + InitDB + ';Data Source=' + ServerName;
      finally
        SYSINI.Free;
      end;
    end;procedure SetConnectionString(ServerName, UserName, Password, InitDB: string);
    var
      SYSINI: TINIFile;
      tmpstr: string;
    begin
      SYSINI := TIniFile.Create('DB.INI');
      try
        with SYSINI do
        begin
          WriteString('Database', 'ServerName', ServerName);
          WriteString('Database', 'UserName', UserName);
          WriteString('Database', 'InitDB', InitDB);
          tmpstr := Encrypt(Password, Key);
          WriteString('Database', 'Password', tmpstr);
        end;
      finally
        SYSINI.Free;
      end;
    end;
      

  2.   

    怎么使用UDL文件
    How to use ADO Connection's ConnectionString property in a flexible way using .udl files. 
    Product:Delphi 4.x (or higher) 
    Uploader: Marcus Neves 
    Question/Problem/Abstract:The ConnectionString property of the ADO Connection object is indeed very flexible, as long as you use IDataInitialize interface, which handles the Microsoft Data Link file (".udl"). 
    Answer:
    <> Abstract <> The ConnectionString property of the ADO Connection object is indeed very flexible, as long as you use IDataInitialize interface, which handles the Microsoft Data Link file (".udl") -- which is an ".ini" like file with just one section. 
    <> Microsoft Data Link Files <> The problem of creating and configuring a connection to a database using a UDL file is much more user-friendly than just using a simple .ini file, because Windows automatically recognizes a .udl file and opens up the "Data Link Properties" dialog box. To create a new .udl file is just a few seconds away: right-clicking on the Windows Explorer allows the user to select New and then Microsoft Data Link to create a .udl file. To edit it, just double-click the file to open up the properties dialog. 
    <> Creating a .udl file programmatically <> OLE-DB, which is the low-level layer used by ADO, has an interface named IDataInitialize that can be used to create a data source object using a connection string and also retrieve a connection string from an existing data source object. The interface has a method called WriteStringToStorage that we can use to write a default .udl file, if none is found. So, for instance, our MyApp application can have a default MyApp.udl file in the same directory and we use this file to connect to the database freeing the application from being recompiled to alter the database which its point to, allowing it to be database-independent (as long as you do not use any SQL specifc to a database). The sample procedure below will show how to use the method: ---------------------------------------------------------- // NOTE: the sample below uses unit files OLEDB and ComObj. procedure WriteUDLFile (const UDLFile: string); 
    const 
      // Default ConnectionString used by our application (SQL Server) 
      SConnect = 'Provider=SQLOLEDB.1;User ID=sa;Initial Catalog=OurExampleDB;' + 
                 'Data Source=OURSERVER;Packet Size=4096'; 
    var 
      DataInitialize: IDataInitialize; 
      wUDLFile: array[0..MAX_PATH - 1] of WideChar; 
    begin 
      // Create the DataInitialize object 
      DataInitialize := CreateComObject(CLSID_DataLinks) as IDataInitialize;   // Convert AnsiString parameter to WideChar 
      StringToWideChar (UDLFile, @wUDLFile, MAX_PATH);   // Call method WriteStringToStorage with the default ConnectionString above. 
      if Failed(DataInitialize.WriteStringToStorage(wUDLFile, SConnect, CREATE_NEW)) then 
        raise Exception.Create('Can''t write UDL'); 
    end; ---------------------------------------------------------- 
    <> Delphi's VCL support <> I've just discovered that the Delphi VCL already comes with a procedure named CreateUDLFile that creates a basic .udl file (with just Provider and Data Source defined). The above code is very similar (was done with no knowledge of Delphi's own routine). If you take a look at the source code of ADODB.pas, you'll encounter the following public routines: procedure CreateUDLFile(const FileName, ProviderName, DataSourceName: WideString); 
    function DataLinkDir: string; 
    procedure GetProviderNames(Names: TStrings); 
    function PromptDataSource(ParentHandle: THandle; InitialString: WideString): WideString; 
    function PromptDataLinkFile(ParentHandle: THandle; InitialFile: WideString): WideString; 
    function GetDataLinkFiles(FileNames: TStrings; Directory: string = ''): Integer; I've found no documentation on the above procedures, but looking at the source code we can more precisely defines what it does. I'll comment briefly on each one here; but if you want more inside information, I'll reinforce you to look at the source code. It's the best way to learn! ** CreateUDLFile: This procedure creates a .udl file with just two properties: Provider and Data Source. ** DataLinkDir: Gets from the registry the default path to save .udl files, defined by Microsoft OLE-DB. ** GetProviderNames: Returns all the names of the Providers available on the system. ** PromptDataSource: Shows the Data Link Properties dialog box to enable the user to edit the ConnectionString easily. The ParentHandle parameter allows the dialog to be centered within a given window handle, which can be your main form (Handle property). ** PromptDataLinkFile: Opens the Select Data Link dialog box. Allows the user to browse and organize .udl files. Returns a fully qualified path to the user-selected .udl file. ** GetDataLinkFiles: Returns all found .udl files in the Directory given. 
    <> Using a .udl file <> To use a .udl file, just pass "File Name=MyUdlFile.udl" string to the DatabaseConnect property of the ADODatabase as show in the procedure below: ---------------------------------------------------------- procedure ConnectFromUDL (const UDLFile: String); 
    begin 
      ADODatabase.Close; 
      ADODatabase.DatabaseConnect := Format('File Name=%s', [UDLFile]); 
      ADODatabase.Open; 
    end; ---------------------------------------------------------- 
    <> Conclusion <> OLE-DB and ADO are really a very good way to deal with different data sources and also allows the programmer to be very flexible dealing with them. This article here shows how to use .udl files to dynamically handles the connection to a database. Another alternative is to present the user with a dialog box where he/she can enter the server name, user name and password and dynamically create the ConnectionString property and use it to connect to the database. But using .udl files allow more flexibility as the user can deal with a Windows default dialog (the same as the ODBC dialog was in the past).
      

  3.   

    打开你的存放连接那个DFM文件,把ADOCONNECTION的CONNEDTED改成FALSE、
    把ADOQUERY、ADODATASET等的ACTIVE改成FALSE
      

  4.   

    讲的太多了,把程序用文本文件打开,把所有的ACTIVE 置为FALSE
    保存就行了
      

  5.   

    如果在delphi环境中,可以直接修改connectionstring
      

  6.   

    把有adoconnection的dfm文件用记事本打开,找到adoconnection的connected项,把其设为false即可
      

  7.   

    我把adoconnection的connected项设为false,打开表单,没有错误提示,然后过了一会儿,还是自动把delphi关闭了,哎,怎么办
      

  8.   

    应该可以的,要不就安装delphi6的sp2吧
      

  9.   

    gzllich(刚从泥坑里出来);
    delphi6的sp2在哪下载?谢谢!
      

  10.   

    安装补丁是不行的。这和delphi6没什么关系, 主要是你的sql server
    数据的问题, 当你现在用的服务器和原来的服务器名称不相同的时候这样就sql server就连接不上的。 如果你把现有的服务器名称改为你在家
    里那时用的。 这样服务器也起不来,需要把你单位的电脑重新改名为你家里的电脑。重新装sql server. 如果你不想这么干的话,那就照楼上的兄弟说的做。把连接字符串写入.ini
    文件,或者是注册表 (用户名,服务器/ip,密码),我常用的方法是注册表。在程序用的时候读出来连接起来送给adoconnection.adoconstring属性
      

  11.   

    zhptj(北狼):
    别吓我哪:(我这是要交去应聘的,急得很,唉,不过我看我这水平,多半那个招聘单位也不敢要了,才学两天,赶鸭子上架,哎
    实在不行的话,今天早上只有在单位不做了,回家再做,还是希望解决哪
    等待中
      

  12.   

    连接最好都在程序启动时打开,设计期编译或保存前关闭ADOCONNEDTION或是先开有ADOCONNECTION的窗体或数据模块就不会有这种问题了
    其实原先DELPHI5下没有这个问题的
      

  13.   

    出来了,大概还是我开始没理解,我只把我打不开的那个表单的active设为false了,应该要把工程里的都改了,
    等我试试第二个问题大家的答案,再结贴
      

  14.   

    To: debussy(debussy) 
        受教。
    To: liuri(璇玑)
        以上意见是正确的,先断开所有的连接。ADOconnection->connected、ADOdatabase-〉active .重新设置ADOconnection->connectstring即可。