我想实现oracle中的启动或关闭数据库功能。在cmd中的操作如下按提示操作输入就可以实现了。
C:\>sqlplus /nologSQL*Plus: Release 9.2.0.1.0 - Production on 星期五 4月 16 17:46:53 2004Copyright (c) 1982, 2002, Oracle Corporation.  All rights reserved.SQL> connect system/11111@aaa as sysdba
已连接到空闲例程。
SQL> startup
ORACLE 例程已经启动。Total System Global Area  135338868 bytes
Fixed Size                   453492 bytes
Variable Size             109051904 bytes
Database Buffers           25165824 bytes
Redo Buffers                 667648 bytes
数据库装载完毕。
数据库已经打开。
SQL>
说明:其中sqlplus /nolog
connect system/king_test@oanet as sysdba
shutdown transactional
这三行(注意是多行而非一行)为我必须手工模拟输入的字符串。并且这个sql语句不能组装在一个语句中完成。我想通过程序来实现。请问如何能实现呢?我记得好象可以用管道之类的来和控制台进行通信,或者是否可以用批处理来实现。不过我一时没找到。请各位提供些解决方案或代码。谢谢!

解决方案 »

  1.   

    type
      TForm1 = class(TForm)
        Editor: TMemo;
        procedure EditorMouseDown(Sender: TObject; Button: TMouseButton;
          Shift: TShiftState; X, Y: Integer);
        procedure EditorKeyDown(Sender: TObject; var Key: Word;
          Shift: TShiftState);
        procedure FormCreate(Sender: TObject);
        procedure FormDestroy(Sender: TObject);
        procedure EditorKeyPress(Sender: TObject; var Key: Char);
      private
        CreateOk: Boolean;
        WPos: TPoint;
        hReadPipe, hWritePipe, hWriteFile, hReadFile: THandle;
        processinfo: PROCESS_INFORMATION;
        procedure SendCmdToShell(const CmdStr: string);
        function GetCmdStr: string;
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.EditorMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    var
      ECharPos: TPoint;
    begin
      ECharPos := Editor.CaretPos;
      if ECharPos.Y > WPos.Y then
        Editor.ReadOnly := False
      else if (ECharPos.Y = WPos.Y) and (ECharPos.X > WPos.X) then
        Editor.ReadOnly := False
      else
        Editor.ReadOnly := True;
    end;function TForm1.GetCmdStr: string;
    var
      LastLine: Integer;
    begin
      LastLine := Editor.Lines.Count - 1;
      if LastLine > WPos.Y then
      begin
        result := Editor.Lines[LastLine];
      end
      else if LastLine = WPos.Y then
      begin
        result := Editor.Lines[LastLine];
        result := Copy(result, WPos.X + 2, Length(result));
      end
      else
      begin
        result := ' ';
      end;
    end;procedure TForm1.SendCmdToShell(const CmdStr: string);
    var
      ShellCmdStr: array[0..256] of char;
      RBuffer: array[0..25000] of char;
      nByteToWrite: DWORD;
      nByteWritten: DWORD;
      nByteReaden: DWORD;
    begin
      if CreateOK then
      begin
        StrPCopy(ShellCmdStr, CmdStr);
        nByteToWrite := StrLen(ShellCmdStr);
        ShellCmdStr[nByteToWrite] := #13;
        ShellCmdStr[nByteToWrite + 1] := #10;
        ShellCmdStr[nByteToWrite + 2] := #0;
        Inc(nByteToWrite, 2);
        WriteFile(hWriteFile, ShellCmdStr, nByteToWrite, nByteWritten, nil);
        Sleep(400);
        Editor.Lines.Clear;
        FillChar(RBuffer, Sizeof(RBuffer), #0);
        ReadFile(hReadFile, RBuffer, 25000, nByteReaden, nil);
        Editor.Lines.Add(StrPas(RBuffer));
        WPos.Y := Editor.Lines.Count - 1;
        WPos.X := Length(Editor.Lines[WPos.Y]) - 1;
      end;
    end;procedure TForm1.EditorKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    var
      ECharPos: TPoint;
    begin
      ECharPos := Editor.CaretPos;
      if ECharPos.Y > WPos.Y then
        Editor.ReadOnly := False
      else if (ECharPos.Y = WPos.Y) and (ECharPos.X > WPos.X) then
      begin
        Editor.ReadOnly := False;
      end
      else
        Editor.ReadOnly := True;
    end;procedure TForm1.FormCreate(Sender: TObject);
    var
      Pipeattr: SECURITY_ATTRIBUTES;
      ShellStartInfo: STARTUPINFO;
      shellstr: array[0..256] of char;
      RBuffer: array[0..25000] of char;
      I: Integer;
      nByteReaden: DWORD;
    begin
      CreateOK := False;
      I := 0;
      Editor.ReadOnly := False;
      Wpos.X := 0;
      WPos.Y := 0;
      with Pipeattr do
      begin
        nLength := Sizeof(SECURITY_ATTRIBUTES);
        lpSecurityDescriptor := nil;
        bInheritHandle := true;
      end;  if CreatePipe(hReadPipe, hWriteFile, @Pipeattr, 0) then
        Inc(i);
      if CreatePipe(hReadFile, hWritePipe, @pipeattr, 0) then
        Inc(i);  GetStartupInfo(ShellStartInfo);
      with ShellStartInfo do
      begin
        dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
        hStdInput := hReadPipe;
        hStdError := hWritePipe;
        hStdOutput := hWritePipe;
        wShowWindow := SW_HIDE;
      end;
      GetSystemDirectory(@Shellstr, MAX_PATH + 1);
      StrCat(@ShellStr, Pchar('\\cmd.exe'));
      if CreateProcess(Shellstr, nil, nil, nil, True, 0,
        nil, nil, ShellStartInfo, processinfo) then
      begin
        Inc(i);
      end
      else
      begin
        MessageBox(Handle, Pchar('?ノShell??'), Pchar('??'), (MB_OK or MB_ICONERROR));
      end;
      if i = 3 then
      begin
        CreateOK := True;
        Editor.Lines.Clear;
        sleep(250);
        ReadFile(hReadFile, RBuffer, 25000, nByteReaden, nil);
        Editor.Lines.Add(StrPas(RBuffer));
        WPos.Y := Editor.Lines.Count - 1;
        WPos.X := Length(Editor.Lines[WPos.Y]) - 1;
      end;
    end;procedure TForm1.FormDestroy(Sender: TObject);
    var
      shellexitcode: Cardinal;
    begin
      if GetExitCodeProcess(processinfo.hProcess, shellexitcode) then
      begin
        if shellexitcode = STILL_ACTIVE then
          TerminateProcess(processinfo.hProcess, 0);
      end;
      if hWriteFile <> 0 then
        CloseHandle(hWriteFile);
      if hReadFile <> 0 then
        CloseHandle(hReadFile);
    end;procedure TForm1.EditorKeyPress(Sender: TObject; var Key: Char);
    var
      ECharPos: TPoint;
    begin
      if Key = Chr(VK_RETURN) then
      begin
        //    ShowMessage(GetCmdStr);
        SendCmdToShell(GetCmdStr);
      end
      else if Key = Chr(VK_BACK) then
      begin
        ECharPos := Editor.CaretPos;
        if ECharPos.X = WPos.X + 1 then
          Key := #0;
      end;
    end;end.