我用ADO 连接数据库,程序如下:
procedure TForm1.Button1Click(Sender: TObject);
var str:string;
begin
  str:='Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;
   Initial Catalog=DBFANG;
   Data Source=CW01';
  ADOConnection1.connected:= false;
  ADOConnection1.ConnectionString :=str;
  ADOConnection1.connected:=true;
end;
我的计算机名字叫CW01, 我要连接的数据库的名字是DBFANG,
为什么我一运行到给STR赋值的时候就出错了呢?

解决方案 »

  1.   

    另外接着问一下怎么能动态取到我的计算机名字并赋值给给连接语句呢?比如说我的计算机名字是CW01,那如果我名字改成CW02了,不就不能用了么,怎么写才可以啊 ?
      

  2.   

    ‘Provider=SQLOLEDB.1;Password=sa;Persist Security Info=True;User ID=sa;Initial Catalog=tempdb;Data Source=CW01‘
      

  3.   

    拿计算机名用api:
      GetComputerName();BOOL GetComputerName(    LPTSTR lpBuffer, // address of name buffer 
        LPDWORD nSize  // address of size of name buffer 
       );
      

  4.   

    procedure TForm1.FormCreate(Sender: TObject);
    var
       wVersionRequested : WORD;
       wsaData : TWSAData;
       p : PHostEnt;
       s : array[0..128] of char;
    begin
         wVersionRequested := MAKEWORD(1, 1);
         WSAStartup(wVersionRequested, wsaData);
         GetHostName(@s, 128);
         p := GetHostByName(@s);
         //ShowMessage(Format('Computer name is ''%s''.', [p^.h_Name]));
         edit2.Text:=s;
    end;
      

  5.   

    一般这种问题是经过INI文件来解决的,通过修改INI文件来指向不同的计算机
      

  6.   

    当然,你的INI文件最好经过加密比较安全!
      

  7.   

    ----------------------------------------------------为什么我一运行到给STR赋值的时候就出错了呢?-----------------------------------------------------怎么会在这一步就出问题呢?
      

  8.   

    如果连本机就用 localhost 或者127.0.0.1 比较方便
      

  9.   

    Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=lin;Data Source=127.0.0.1str:='Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;
       Initial Catalog=DBFANG;你的str是不是少点东西啊
    应该加上datasource=127.0.0.1  //连接的服务器地址
    Initial Catalog=DBFANG;//这个是连数据库的名字