精华区 [关闭][返回]

当前位置:网易精华区>>讨论区精华>>编程开发>>● Delphi>>数据库技术>>怎么写一个程序自动配置odbc

主题:怎么写一个程序自动配置odbc
发信人: surebeijing(量子虫洞)
整理人: teleme(2001-07-29 19:38:25), 站内信件
【 在 lion_jiang 的大作中提到:】
:
:怎么写一个程序自动配置odbc???(数据库与程序不在同一台机子上)
:我用的数据库是sql sever 数据别名是server
:我希望写一个程序或者控件让用户自己填上计算机名与数据库用户名和密码就能连上。
:怎么写一个程序备份SQL的数据(最好还有恢复数据)??(程序和sql在同一台机子上)
:
:......
 


{建议你通过ADO进行存取,这是远程连接Sql server的最好方式
入下两个函数:
成功返回ADO的连接对象,否则返回nil。
你可检测其返回值是否可用。

Example:

procedure TForm1.FormCreate(Sender: TObject);
begin
  ADODataSet1.CommandText := 'SELECT * FROM Customers';
  //ADODataSet1.Connection := Connect_SQLUser('RemoteServer','Yourname','123456','RSqlServer','NorthWind');
  ADODataSet1.Connection := Connect_SQLUser('YttHome','yttriumok','841668','YttHome','NorthWind');
  if ADODataSet1.Connection <> nil then
    ADODataSet1.Open;
end;


}



implementation
uses DB, ADODB;

{方式一:仅通过NT账号登陆}
function Connect_NTUser(psMachine,psSQLServer,psDataBase:string):TADOConnection;
begin
  psDataBase := trim(psDataBase);
  psMachine := trim(psMachine);
  psSQLServer := trim(psSQLServer);

  result := TADOConnection.Create(nil);
  result.ConnectionString :=
    'Provider=SQLOLEDB.1;Integrated '+
    'Security=SSPI;Persist Security Info=False;'+
    'Initial Catalog='+ psDataBase +
    ';Data Source=' + psSQLServer +
    ';Use Procedure for Prepare=1;Auto Translate=True;'+
    'Packet Size=4096;Workstation ID='+ psMachine +
    ';Use Encryption for Data=False;Tag with column collation when possible=False';
  result.LoginPrompt := false;
  try
    result.Connected:=true;
  except
    on e:Exception do
    begin
      result.Free;
      result := nil;
      exit;
    end;
  end;
  if not result.Connected then
  begin
    result.Free;
    result := nil;
  end;
end;

{方式二:仅通过SQL内置账号登陆——注意:这时对应的SQL Server的“身份
验证属性”必须为——Sql server和Window(s),否则通不过}
function Connect_SQLUser(psMachine,psUser,psPassword,psSQLServer,psDataBase:string):TADOConnection;
begin
  psDataBase := trim(psDataBase);
  psMachine := trim(psMachine);
  psSQLServer := trim(psSQLServer);
  psUser := trim(psUser);
  psPassword := trim(psPassword);

  result := TADOConnection.Create(nil);
  result.ConnectionString :=
  'Provider=SQLOLEDB.1;'+
  'Password=' + psPassword  +
  ';Persist Security Info=True;User ID=' + psUser +
  ';Initial Catalog=' + psDataBase +
  ';Data Source=' + psSQLServer +
  ';Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID='+psMachine+
  ';Use Encryption for Data=False;Tag with column collation when possible=False';
  result.LoginPrompt := false;
  try
    result.Connected:=true;
  except
    on e:Exception do
    begin
      result.Free;
      result := nil;
      exit;
    end;
  end;
  if not result.Connected then
  begin
    result.Free;
    result := nil;
  end;
end;

// END


----
请问你要到那个宇宙去?

[关闭][返回]