mainado连接数据库

procedure TMainDataModule.DataModuleCreate(Sender: TObject);
var
  strConn :String;
begin
  StrConn := 'Provider=SQLOLEDB.1;';
  strConn := strConn + 'Data Source=.;Initial Catalog=Book;User ID=sa;Password= ;';

  if MainADO.connected then MainADO.connected := False;
  MainADO.ConnectionString := strConn;
  try
    MainADO.connected := True;
  except
    Application.MessageBox('数据库连接失败,请查看电脑上数据库中的设置是否正确!'
      , pchar('启动错误'), mb_iconInformation + mb_ok);
    Halt(0);
  end;
end;

procedure TMainDataModule.DataModuleDestroy(Sender: TObject);
begin
  MainADO.Connected := false;
end;

procedure CreateQuery;
begin
  if DOCQuery = nil then
  begin
    DOCQuery := TADOQuery.Create(nil);
    DOCQuery.Connection := GetConnection;
  end
  else
    DOCQuery.Close;
end;

function GetConnection: TADOConnection;
begin
  if Assigned(FDocConnection) then
    Result := FDocConnection
  else
  begin
    Result := TADOConnection.Create(nil);
    Result.ConnectionString := MainDataModule.MainADO.ConnectionString;
    Result.ConnectionTimeout := 1000;
    Result.LoginPrompt := False;
    Result.Connected := True;
    FDocConnection := Result;
  end;
end;

你可能感兴趣的:(数据库)