我在本机上做了一个程序,后面是SQL,但这个后台数据库不在本机上,而是安装在另外一台电脑上。
我该如何让我这个程序连接另外那台电脑的SQL上呢?

解决方案 »

  1.   

    ADOQuery.ConnectionString :=
                'Provider=Microsoft.Jet.OLEDB.4.0;Password="";' +
                'Data Source=' + path +
                ';Persist Security Info=True';仔细研究一下就知道 连接串中可以写机器网络的名字 也可以是IP地址 
    ------------------------------------------------------------------------------
    金盆洗澡    重出江湖     打劫.抢分   掀起一场腥风血雨   戒烟攒钱 只为换新车
      

  2.   

    设置AdoQuery的ConnectionString属性既可以了!
      

  3.   

    我按照楼上的二位试了,但出现SQL SERVER不存在或拒绝访问
      

  4.   

    你可以在本机上上设置odbc,然后用querry 连接就可以了~不过这样的缺点是发布程序的时候要发布bde并且进行注册标的修改~
    你可以这样设置odbc,通过注册表
    procedure Tdataset_.Button1Click(Sender: TObject);
    var reg:tregistry;
    begin
      reg:=tregistry.Create;
      reg.RootKey:=HKEY_CURRENT_USER;
      reg.CloseKey;
      if reg.OpenKey('Software\ODBC\ODBC.INI\ODBC Data Sources',true) then
      begin
        reg.WriteString('databasename','SQL Server');
        reg.CloseKey;
      end
      else
        form1.showtext('您没有操作系统的管理员权限');
      if reg.OpenKey('Software\ODBC\ODBC.INI\WLXT',true) then
        begin
          reg.WriteString('Database','WLXT');
          reg.WriteString('Driver','C:\WINDOWS\System32\SQLSRV32.dll');
          reg.WriteString('LastUser','sa');
          reg.WriteString('Server','server  name');
          form1.showtext('完成!请点击测试进行测试!');
          button2.SetFocus;
        end
      else
        form1.showtext('您没有操作系统的管理员权限');
      button2.Enabled:=true;
      reg.Free;
    end;
      

  5.   

    如果用adoquery进行连接的话,比较方便和简单,但是服务器的名称只能用机器名其实动态ip也是由ip的,
    运行 cmd
    ipconfig /all
    就可以看到ip乐~
    你把服务器的地址改成这个ip肯定没有问题~不过下次登陆网络的时候又要改~比较麻烦
      

  6.   

    ADODatabase.ConnectionString :=
                'Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;'
                                                                      ------
                 +'Initial Catalog=anyisys;Data Source=WK1'
                             --------------          -----
      

  7.   

    你说你的SQL数据库在别人的机子上,请问你的两台机子是否有相连?是不是局域网的两台机子呀?
    如果是的话,我想很简单,ADOQuery,在静态设置连接字符串时,他会列出你整个局域网内的所有SQL数据库服务器,你只要选择你想要的就行了。
      

  8.   

    我是这么写的,请各位帮忙看一下
            InfoFile:=TIniFile.Create(GetCurrentDir()+'\login.ini');
            server:=InfoFile.ReadString('server','server',s);
            database:=InfoFile.ReadString('database','database',s);
            user:=InfoFile.ReadString('user','user',s);
            password:=InfoFile.ReadString('password','password',s);
    //////////////////
            Produce.ConnectionString:='Provider=SQLOLEDB.1;Persist Security Info=False;'
            +'user id='+user+';'
            +'Password='+password+';'
            +'Initial Catalog='+database+';'
            +'Data Source='+server;
            produce.Open();
            produce.Close();/////////////下面是INI文件////////////
    [server]
    server=IBMCZJ
    [database]
    database=AIS20050602140751
    [user]
    user=sa
    [password]
    password=
      

  9.   

    网络都是通的,但我一运行就提示,SQL SERVER不存在或拒绝访问
      

  10.   

    先建立信任连接,就是先通过机子访问一个服务器的文件,you know?
      

  11.   

    以上的兄弟,都讲得差不多了,呵要,我补充一点
    完善: 连接时还要涉及到 SQL Server的端口,默认是1433,但也有可能出现其它的,呵呵,所以嘛,连接时应该还加个端口,会更好点呵呵
      

  12.   


    procedure TdtmGlobal.conMainBeforeConnect(Sender: TObject);
      function GetConnectStr(var ServerExists: boolean): string;
      var
        ServerMachine, UserID, Password, DBName: string;
      begin
        ServerMachine := ReadRegString(RootPath, 'Server');
        if SameText(ServerMachine, '') then
          ServerExists := False
        else ServerExists := True;
        UserID := ReadRegString(RootPath, 'UserID');
        Password := ReadRegString(RootPath, 'Password');
        if Password = '' then
          Password := ''
        else
          Password := Mydecrypt(Password, MyKey);
        DBName := ReadRegString(RootPath, 'DBName');    Result := 'Provider=SQLOLEDB.1;' +
          'Password=' + Password + ';' + 'Persist Security Info=True;' +
          'User ID=' + UserID + '; ' + 'Initial Catalog=' + DBName + ';' +
          'Data Source=' + ServerMachine;
      end;
    var
      AServerExists: boolean;
      ConnectStr: string;
    begin
      conMain.Connected := False;
      ConnectStr := GetConnectStr(AServerExists);
      if AServerExists then
      begin
        conMain.ConnectionString := ConnectStr;
        conMain.DefaultDatabase := '';
      end;
    end;
      

  13.   

    procedure WriteRegString(KeyPath, KeyName, KeyValue: string);
      var
        Reg: TRegistry;
      begin
        Reg := TRegistry.Create;
        try
          Reg.RootKey := HKEY_CURRENT_USER;
          if Reg.OpenKey(KeyPath, True) then
            Reg.WriteString(KeyName, KeyValue);
        finally
          FreeAndNil(Reg);
        end;
      end;
    begin
      inherited;
      WriteRegString(RootPath, 'Server', edtServer.Text);
      WriteRegString(RootPath, 'DBName', edtDBName.Text);
      WriteRegString(RootPath, 'UserID', edtUserID.Text);
      if edtPassword.Text = '' then
        WriteRegString(RootPath, 'Password', '')
      else
        WriteRegString(RootPath, 'Password', Myencrypt(edtPassword.Text, SoftKey));
      

  14.   

    高深~高深~高深~高深~
    accidence
    accidence
      

  15.   

    现在很多都是用ADO做数据库程序,可以用ADOQUERY控件的ConnectString属性,建立与SQL数据库的联接。具体可以按照提示操作就可以了。