在SQLConnection1,如果通过属性连接了数据库,delphi自动产生的代码如下:
  object SQLConnection1: TSQLConnection
    ConnectionName = 'MSSQLConnection'
    DriverName = 'MSSQL'
    GetDriverFunc = 'getSQLDriverMSSQL'
    LibraryName = 'dbexpmss.dll'
    LoginPrompt = False
    Params.Strings = (
      'DriverName=MSSQL'
      'HostName=192.168.0.200'
      'DataBase=spdata'
      'User_Name=sa'
      'Password=test'
      'BlobSize=-1'
      'ErrorResourceFile='
      'LocaleCode=0000'
      'MSSQL TransIsolation=ReadCommited'
      'OS Authentication=False')
    VendorLib = 'oledb'
    Connected = True
  end请问:假如我要在模块创建时就要通过代码连接数据库,该如何写代码???

解决方案 »

  1.   

    因为我的连接数据库的信息写在*.ini文件里的。所以要通过代码写连接,不能通过属性写。
    有谁能帮我吗?
      

  2.   

    procedure TForm1.Button1Click(Sender: TObject);
    begin
      SQLConnection1.Params.Text:='DriverName=MSSQL'+#13+
                                   'HostName=192.168.0.1'+#13+
                                   'DataBase=dds'+#13+
                                   'User_Name=sa'+#13+
                                   'Password=sa'+#13+
                                   'BlobSize=-1'+#13+
                                   'ErrorResourceFile='+#13+
                                   'LocaleCode=0000'+#13+
                                   'MSSQL TransIsolation=ReadCommited'+ #13+
                                   'OS Authentication=False';   SQLConnection1.ConnectionName:='111111';
       SQLConnection1.DriverName:='MSSQL';
       SQLConnection1.GetDriverFunc:= 'getSQLDriverMSSQL';
       SQLConnection1.LibraryName:='dbexpmss.dll';
       SQLConnection1.VendorLib:='oledb';
       SQLConnection1.LoginPrompt:=false;
       try
         SQLConnection1.Connected:=true;
         Showmessage('连接成功');;
       except
         Showmessage('连接失败');
       end;end;
      

  3.   

    这是动态连接的效果
    如果要静态连接,请双击SQLConnection1,再一一设置属性
      

  4.   

    看一下帮助文档吧,用LoadParamsFromIniFile函数。
      

  5.   

    procedure TForm1.Button1Click(Sender: TObject);
    begin
      SQLConnection1.Params.Text:='DriverName=MSSQL'+#13+
                                   'HostName=192.168.0.200'+#13+
                                   'DataBase=spdata'+#13+
                                   'User_Name=sa'+#13+
                                   'Password=test'+#13+
                                   'BlobSize=-1'+#13+
                                   'ErrorResourceFile='+#13+
                                   'LocaleCode=0000'+#13+
                                   'MSSQL TransIsolation=ReadCommited'+ #13+
                                   'OS Authentication=False';   SQLConnection1.ConnectionName:='111111';
       SQLConnection1.DriverName:='MSSQL';
       SQLConnection1.GetDriverFunc:= 'getSQLDriverMSSQL';
       SQLConnection1.LibraryName:='dbexpmss.dll';
       SQLConnection1.VendorLib:='oledb';
       SQLConnection1.LoginPrompt:=false;
       try
         SQLConnection1.Connected:=true;
         Showmessage('连接成功');;
       except
         Showmessage('连接失败');
       end;end;
      

  6.   

    DescriptionCall LoadParamsFromIniFile to ensure that the DriverName and Params properties accurately reflect the connection configuration for the ConnectionName property that is stored in the file specified by the AFileName parameter. If the AFileName parameter is omitted, LoadParamsFromIniFile uses the dbxconnections.ini file. Otherwise, it is a fully-qualified path name to a file that has the same format as the dbxconnections.ini file. Typically, this file is created by making a copy of the dbxconnections.ini file under a different name.In most cases, it is unnecessary to load connection configurations at runtime because when you set ConnectionName at design time, the DriverName and Params properties are automatically set as well. However, LoadParamsFromIniFile lets you use a customized set of connection configurations at runtime that differ from those stored in the dbxconnections.ini file, or to temporarily override the DriverName or connection parameter values at design time and then reestablish the defaults at runtime.
      

  7.   

    参照dbxconnections.ini的样子改写,然后重命名