ADOConnection1.ConnectionString:=
        ' Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;User ID=mjb;Initial Catalog=dbStudentInfo;Data Source=MJB ';
end;上面User ID  ,Data Source 会随着机器(就是放到别的机器上运行)的变化而变化,请问把它们设置为变量怎么做?怎么获得登陆服务器的用户名和数据源

解决方案 »

  1.   

    如果不要考虑安全的话可以考虑放到ini文件里面
    然后在程序初始化的时候读取着两个东东
      

  2.   

    写一个函数进行设置
    function SetADOConnection(user_id,datasource:string):boolean;
    var
    s:string;
    begin
      with ADOConnection1 do
      begin
       //可以先检测一下,是否有连接到其它数据库且应用程序正在使用中。
      s:=' Provider=SQLOLEDB.1;Integrated Security=SSPI;' 
      s:=s+Persist Security Info=False;User ID='
      s:=s+user_id+';Initial Catalog=dbStudentInfo;Data Source='+datasource
      connected:=false;
      ConnectionString:=s;
      connected:=true;   end;可以在表单上放两个EDIT来输入参数。可把连接设置好后用文本方式打开表单,拷贝出连接字串具体自己写吧
      

  3.   

    修正:
    function SetADOConnection(user_id,datasource:string):boolean;
    var
    s:string;
    begin
      with ADOConnection1 do
      begin
       //可以先检测一下,是否有连接到其它数据库且应用程序正在使用中。
      s:=' Provider=SQLOLEDB.1;Integrated Security=SSPI;' 
      s:=s+Persist Security Info=False;User ID='
      s:=s+user_id+';Initial Catalog=dbStudentInfo;Data Source='+datasource
      connected:=false;
      ConnectionString:=s;
      connected:=true;   
     end;
    end;
      

  4.   

    拷,要吃饭了,错误真多。
    function SetADOConnection(user_id,datasource:string):boolean;
    var
    s:string;
    begin
      with ADOConnection1 do
      begin
       //可以先检测一下,是否有连接到其它数据库且应用程序正在使用中。
      s:=' Provider=SQLOLEDB.1;Integrated Security=SSPI;'; 
      s:=s+Persist Security Info=False;User ID=';
      s:=s+user_id+';Initial Catalog=dbStudentInfo;Data Source='+datasource;
      connected:=false;
      ConnectionString:=s;
      connected:=true;   
     end;
    end;
      

  5.   

    不要放EDIT 控件,这样用户用起来麻烦
      

  6.   

    那就写在INI文件里吧,如果SQL服务器改变,只需在客户端设置手动INI文件一次,
    每次启动程序时从文本文件读入数据来设置
    如下:procedure TForm1.FormCreate(Sender: TObject);
    var
      MyFile:textfile;
      S1,s2:string;
    begin
      s2:='';
      ADO1.Connected:=false;
      assignfile(Myfile,'c:\a.txt');
      reset(MyFile);
      try
        while not eof(Myfile) do
        begin
          readln(Myfile,S1);
          if s2='' then s2:=trim(s1)
          else s2=s2+trim(s1);
        end;
        ADOconnectin1.ConnectionString:=S2;
        ADO1connection.Connected:=true;
      finally
        closefile(Myfile);
      end;end;end.
    大致如上,自己测试一下(可把连接字串拷贝到文本文件中,可分作几行)
      

  7.   

    怎么搞的,上面有有错误:
    procedure TForm1.FormCreate(Sender: TObject);
    var
      MyFile:textfile;
      S1,s2:string;
    begin
      s2:='';
      ADO1.Connected:=false;
      assignfile(Myfile,'c:\a.txt');
      reset(MyFile);
      try
        while not eof(Myfile) do
        begin
          readln(Myfile,S1);
          if s2='' then s2:=trim(s1)
          else s2:=s2+trim(s1);
        end;
        ADOconnectin1.ConnectionString:=S2;
        ADO1connection.Connected:=true;
      finally
        closefile(Myfile);
      end;end;end.
      

  8.   

    难道不能让这样吗ADOConnection1.ConnectionString:=
            ' Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;User ID=mjb;Initial Catalog=dbStudentInfo;Data Source=MJB ';
    end;
    要变的就是 User ID   和Data Source
    我让  User ID =一个能得到用户名的函数,Date Source =GetComputerName(因为上面的MJB其实是我的机器名),可以吗
      

  9.   

    MJB 也是sql server 服务器名
      

  10.   

    真的搞不懂你的意思了
    我分析你的需求不外两种
    1、你的数据库有多个(相同数据库相当于多子公司的ERP),应用程序要在不同
      公司间切换用我前面的方法可以搞定;
    2、你的数据库服务器机器名或数据库名可能会改变,后面的方法也可搞定。
     可能你的意思是数据库服务器机器名或数据库名改变后自动获取,但USERID、PASSWORD也可自动获取吗,如果有多数据库如何处理?