⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainunit.cpp

📁 Direct Oracle Access 非常好的Oracle数据库直接访问组件包 支持个版本的Delphi及C++ Builder 有源码
💻 CPP
字号:
// Direct Oracle Access - LongRaw
// Allround Automations
// support@allround.automations.nl
// http://www.allroundautomations.nl
//
// This application demonstrates:
// - Reading and writing Long Raw columns
//---------------------------------------------------------------------------
#include <vcl\vcl.h>
#pragma hdrstop

#include "Mainunit.h"
#include <stdlib.h>
//---------------------------------------------------------------------------
#pragma link "Grids"
#pragma link "Oracle"
#pragma resource "*.dfm"
TMainForm *MainForm;
//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ClearData()
{
  int r, c;

  for (c = 0; c < Grid->ColCount; c++)
    for (r = 0; r < Grid->RowCount; r++)
      Grid->Cells[c][r] = "";
  Grid->ColCount = 2;
  Grid->RowCount = 2;
  ColumnLabel->Caption = "";
  StatusBar->SimpleText = "";
  ImportBtn->Enabled = False;
  ExportBtn->Enabled = False;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::BuildTableList()
{
  TableList->Items->Add("Select a table");
  if (MainSession->Connected)
  {
    TablesQuery->Execute();
    while (!TablesQuery->Eof)
    {
      TableList->Items->Add(TablesQuery->Field("Tab"));
      TablesQuery->Next();
    }
  }
  TableList->ItemIndex = 0;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormCreate(TObject *Sender)
{
  ClearData();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ConnectBtnClick(TObject *Sender)
{
  if (OracleLogon->Execute())
  {
    ClearData();
    TableList->Clear();
    {
      if (MainSession->Connected)
        MainForm->Caption = MainSession->LogonUsername + "@" + MainSession->LogonDatabase;
      else
        MainForm->Caption = "Not connected";
      BuildTableList();
    }
  }
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::TableListChange(TObject *Sender)
{
  int Row, Column, Fld;

  ClearData();
  if (TableList->ItemIndex > 0)
  {
    try
    {
      // Create the SQL statement and execute it
      SelectQuery->SQL->Text = "select t.*, rowid from " + TableList->Text + " t";
      SelectQuery->Execute();
      // Place the fieldnames into the first row of the grid
      if (SelectQuery->FieldCount() - 1 >= Grid->ColCount) Grid->ColCount = SelectQuery->FieldCount() - 1;
      Column = 1;
      for (Fld = 0; Fld < SelectQuery->FieldCount(); Fld++)
      {
        if (SelectQuery->FieldType(Fld) == otLongRaw)
          // Set the name of the Long Raw column
          ColumnLabel->Caption = SelectQuery->FieldName(Fld);
        else
        {
          Grid->Cells[Column][0] = SelectQuery->FieldName(Fld);
          // Save the index of the rowid column
          if (SelectQuery->FieldName(Fld) == "ROWID") RowIdColumn = Column;
          Column++;
        }
      }
      // Place the data into the grid
      Row = 1;
      while (!SelectQuery->Eof)
      {
        Grid->Cells[0][Row] = IntToStr(Row);
        Column = 1;
        for (Fld = 0; Fld < SelectQuery->FieldCount(); Fld++)
        {
          if (SelectQuery->FieldType(Fld) != otLongRaw)
          {
            Grid->Cells[Column][Row] = SelectQuery->Field(Fld);
            Column++;
          }
        }
        Row++;
        SelectQuery->Next();
      }
      // Enable buttons
      ImportBtn->Enabled = (Row > 1);
      ExportBtn->Enabled = (Row > 1);
      // Size the grid
      if (Row > Grid->RowCount) Grid->RowCount = Row;
      // Show number of selected records
      StatusBar->SimpleText = IntToStr(SelectQuery->RowsProcessed()) + " Rows selected";
    }
    catch (EOracleError &E)
    {
    // Show Oracle error in message box and status bar
      ShowMessage(E.Message);
      StatusBar->SimpleText = E.Message;
    }
  }
  Grid->SetFocus();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ImportBtnClick(TObject *Sender)
{
  void *Buffer;
  int FileLength;
  TFileStream *F;

  if (OpenDialog->Execute())
  {
    // Open the file
    F = new TFileStream(OpenDialog->FileName, fmOpenRead);
    FileLength = F->Size;
    Buffer = malloc(FileLength);
    // Read the file into the buffer in one single blockread
    F->Read(Buffer, FileLength);
    F->Free();
    // Create a SQL statement to update the long raw column by rowid
    PutLongRawQuery->SQL->Text = "update " + TableList->Text;
    PutLongRawQuery->SQL->Add("set " + ColumnLabel->Caption + " = :this_column");
    PutLongRawQuery->SQL->Add("where rowid = :this_row");
    // Set the values of the variables
    PutLongRawQuery->SetLongVariable("this_column", Buffer, FileLength);
    PutLongRawQuery->SetVariable("this_row", Grid->Cells[RowIdColumn][Grid->Row]);
    // Execute & commit
    PutLongRawQuery->Execute();
    MainSession->Commit();
  }
  free(Buffer);
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ExportBtnClick(TObject *Sender)
{
  int Offset, Length;
  Byte Buffer[1500];
  TFileStream *F;

  if (SaveDialog->Execute())
  {
    // Create a SQL statement to retrieve the long raw column by rowid
    GetLongRawQuery->SQL->Text = "select " + ColumnLabel->Caption + " from " + TableList->Text;
    GetLongRawQuery->SQL->Add("where rowid = :this_row");
    // Set the variable of the rowid
    GetLongRawQuery->SetVariable("this_row", Grid->Cells[RowIdColumn][Grid->Row]);
    GetLongRawQuery->Execute();
    if (!GetLongRawQuery->Eof)
    {
      // Create the file
      F = new TFileStream(SaveDialog->FileName, fmCreate);
      // Write the array in several blockwrites of 1500 bytes
      Offset = 0;
      do
      {
        Length = GetLongRawQuery->GetLongField(0, &Buffer, Offset, 1500);
        F->Write(&Buffer, Length);
        Offset = Offset + 1500;
      }
      while (Length >= 1500);
      F->Free();
    }
    StatusBar->SimpleText = ColumnLabel->Caption + " exported to " +
                            SaveDialog->FileName;
  }

}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -