代码如下:
procedure TfmMain.LoginADatabase;
var
  Reg:TRegistry;
begin
  Reg:=TRegistry.Create;
  try
    Reg.RootKey:=HKEY_LOCAL_MACHINE;
    if Reg.KeyExists('\SOFTWARE\NEWPIN\Test\') then
    begin
    if Reg.OpenKey('\SOFTWARE\NEWPIN\Test\',false) then
    begin
      if Reg.ReadString('Connection')='Use Windows authentication' then
      try
        ADOConnection1.Close;
        ADOConnection1.ConnectionString:='Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog='+Reg.ReadString('Database Name')+';Data Source='+Reg.ReadString('SQLServer Name');
        ADOConnection1.Open();
        ADOConnection1.KeepConnection:=true;
      except
      if MessageDlg('服务器连接失败!'+#13+'请检查服务器是否处于运行状态或重新填写服务器登录信息。'+#13+#13+'是否重新填写服务器注册信息?',mtWarning,[mbYES,mbNO],0)=mrYES then
      begin
        RegASQL:=TRegASQL.Create(Application);
        RegASQL.ShowModal;
        RegASQL.Update;
      end
      else
      Application.Terminate;
      end;
      if Reg.ReadString('Connection')='Use SQL Server authentication' then
      try
        ADOConnection1.Close;
        ADOConnection1.ConnectionString:='Provider=SQLOLEDB.1;Password='+RegASQL.UnEncrypt(Reg.ReadString('Password'))+';Persist Security Info=True;User ID='+RegASQL.UnEncrypt(Reg.ReadString('User ID'))+';Initial Catalog='+Reg.ReadString('Database Name')+';Data Source='+Reg.ReadString('SQLServer Name');
        ADOConnection1.Open();
        ADOConnection1.KeepConnection:=true;
      except
      if MessageDlg('服务器登陆失败,是否重新注册?',mtWarning,[mbYES,mbNO],0)=mrYES then
      begin
        RegASQL:=TRegASQL.Create(Application);
        RegASQL.ShowModal;
        RegASQL.Update;
      end
      else
      Application.Terminate;
      end;
    end;
    end
    else
    begin
      RegASQL:=TRegASQL.Create(Application);
      RegASQL.ShowModal;
      RegASQL.Update;
    end;
  finally
    Reg.Free;
  end;
end;
谢谢.我必有重谢的

解决方案 »

  1.   

    我是guorui_wh,帮你解决问题的那个,如果还是有什么问题,就找我,qq联系
      

  2.   

    从逻辑和语法看没有程序上没有错误呀!
    to  baby19820917() 
    没有搞错吧!自己写的代码让别人给注解?
    主要就是从注册表中读取数据库联接的字符串进行数据联接!
    其实这段代码还应该改造一下,就是如果注册表中没有该键值或者注册表中的键值不正确(根据注册表中的字符无法联接到数据库)时都应该让用户进行手工输入联接字符串,如果输入的字符串能正联接数据则应该写入到注册表中!