大家好:
    也许这个问题有很多人问拉但我还是不清楚!求求你们在给小弟一点帮助好吗?
请问:用ADO怎样动态的连接SQL Server 2000服务器啊?需要设置那些参数啊?我  
      的服务器名是USER,密码是:666666,我要连接的数据库名是wchb,要连接  
      的数据表是:aaa.
请那位高手用代码给具体的演示一下好吗?
      拜托拜托拉。

解决方案 »

  1.   

    动态连接我想一般也就是在安装程序的时候可以使用到!当ADOConnection控件中建立起ConnectionString后,可以生成对应的udl文件,所以在制作安装包的时候可以自己写一个无窗体的exe文件来读取udl文件的内容,并将内容写入注册表,然后将此字符串赋给ADOConnection控件的对应属性!不知道楼主是否要的是这个功能?
      

  2.   

    请问楼上的朋友,ConnectionString怎么定啊?是ud1自动生成的吗?ud1文件在哪里?谢谢
      

  3.   

    你先在能连上数据库时,用ADOConnection连上数据库,在把这时的ConnectionString拷到你的代码中不就行了吗(想改哪个字段随意)!别想让他自己生成,计算机可笨呢!
      

  4.   

    datebasename := 'wchb';
    servername := 'USER';
    DM.ADOCon.ConnectionString :=' Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;password = 666666;Initial Catalog= '
                               + DataBaseName
                               + ' ;Data Source='
                               + ServerName;
        DM.ADOCon.Connected:=true;
      

  5.   

    引用 AdoConEd 单元就是配置连接字符串时的窗体。将连接字符串保存到注册表或别的什么地方。启动读入,如果连接不正常就让用配置并保存。
      

  6.   

    不用那么麻烦的吧其实连接的方法有很多,可以用ini文件,或别的我自己习惯用udl文件连,这样修改方便,移植、性好 首先你建立一个文本文件,然后将扩展名改为.udl,发布时,先运行这个udl文件,让用户根据自己服务器进行配置
    程序启动时,从这个udl文件中读取连接数据,进行连接
      

  7.   

    调用“数据链接属性”对话框? 
    可以使用UDL文件,UDL文件到打开方式是:Rundll32.exe C:~1~1~1.dll,OpenDSLFile %1uses ADODB;然后使用函数:PromptDataSource即可,该函数原型如下:usesOleDb,ComObj,ActiveX;function PromptDataSource(ParentHandle: THandle; InitialString: WideString): WideString;varDataInit: IDataInitialize;DBPrompt: IDBPromptInitialize;DataSource: IUnknown;InitStr: PWideChar;beginResult := InitialString;DataInit := CreateComObject(CLSID_DataLinks) as IDataInitialize;if InitialString <> '' thenDataInit.GetDataSource(nil, CLSCTX_INPROC_SERVER,PWideChar(InitialString), IUnknown, DataSource);DBPrompt := CreateComObject(CLSID_DataLinks) as IDBPromptInitialize;if Succeeded(DBPrompt.PromptDataSource(nil, ParentHandle,DBPROMPTOPTIONS_PROPERTYSHEET, 0, nil, nil, IUnknown, DataSource)) thenbeginInitStr := nil;DataInit.GetInitializationString(DataSource, True, InitStr);Result := InitStr;end;end; 
      

  8.   

    如何使用INI及UDL文件来保存数据库参数你可以根据不同的数据库参数动态生成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;怎么使用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).
      

  9.   

    Drate(小虫)>>  你是雷锋!  收藏