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

📄 datamod.cpp

📁 《C Builder 5程序设计——数据库应用实务篇》程序源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//---------------------------------------------------------------------------
// Borland C++Builder
// Copyright (c) 1987, 1998 Borland International Inc.  All Rights Reserved.
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include <stdio.h>
#include "Datamod.h"
#include <utilcls.h>
//---------------------------------------------------------------------------
#pragma resource "*.dfm"
TMastData *MastData;
//---------------------------------------------------------------------------
__fastcall TMastData::TMastData(TComponent* Owner)
  : TDataModule(Owner)
{
}
//---------------------------------------------------------------------------
// Utility functions

bool Confirm(const char *Msg)
{
  return ((MessageBox(NULL,Msg, "Confirmation", MB_YESNO) == IDYES)?1:0);
}

AnsiString TMastData::DataDirectory()
{
 //Assume data is in ..\..\data relative to where we are
  AnsiString temp = ExtractFilePath(ParamStr(0));
  return ExpandFileName(temp + "..\\..\\DATA\\");
}

//This function switches the database to a different alias.

void TMastData::SetDatabaseAlias(AnsiString AliasName)
{
  Screen->Cursor = crHourGlass;
  try
  {
    Database->Close();
    Database->AliasName = AliasName;
    Database->Open();
    Screen->Cursor = crDefault;
  }
  catch(...)  {    Screen->Cursor = crDefault;  }
}

// Steps through Items and gathers sum of ExtPrice. After OrdersItemsTotal
//  is calculated, OrdersCalcFields is automatically called (which
//  updates other calculated fields.
void TMastData::UpdateTotals()
{
  Extended TempTotal;
  TBookmark PrevRecord;

  if (!DeletingItems)		// don't calculate if deleting all items
  {
    PrevRecord = Items->GetBookmark();	// returns null if table is empty }
    try
    {
      Items->DisableControls();
      Items->First();
      TempTotal = 0;			// use temp for efficiency
      while (!Items->Eof)
      {
        TempTotal = TempTotal + ItemsExtPrice->Value;
        Items->Next();
      }
      OrdersItemsTotal->Value = TempTotal;
    }
    catch(...)
    {
       Items->EnableControls();
       if (PrevRecord)
       {
         Items->GotoBookmark(PrevRecord);
         Items->FreeBookmark(PrevRecord);
       }
       return;    //do not repeat execution of above code.
    }
    Items->EnableControls(); //if no catch clause do a little cleanup
    if (PrevRecord)
    {
      Items->GotoBookmark(PrevRecord);
      Items->FreeBookmark(PrevRecord);
    }
  }/*end !DeleteingItems*/
}


void TMastData::DeleteItems()
{
  DeletingItems = True;     // Suppress recalc of totals during delete
  Items->DisableControls(); // for faster table traversal.
  try
  {
    Items->First();
    while(!Items->Eof)
     Items->Delete();
  }
  catch(...)
  {
    DeletingItems = False;
    Items->EnableControls();   //always re-enable controls after disabling
    return;
  }
  DeletingItems = False;
  Items->EnableControls();     //always re-enable controls after disabling
}

// Create an alias for the local data if needed, then swith the Database
//  to use it.
void TMastData::UseLocalData()
{
  AnsiString DataDir;
  // See if the target alias exists, if not then add it.
  if (!Session->IsAlias("DBDEMOS"))
  {
    DataDir = DataDirectory();
    if (!FileExists(DataDir+"ORDERS.DB"))
//      raise Exception.Create("Cannot locate Paradox data files");
      MessageBox(NULL,"Cannot locate Paradox data files","Warning",MB_OK);
    Session->AddStandardAlias("DBDEMOS", DataDir, "PARADOX");
  }
  SetDatabaseAlias("DBDEMOS");
}

// Create an alias to point to the MastSQL.GDB file if needed

void TMastData::UseRemoteData()
{
  TStringList * Params = new TStringList;
  AnsiString DataFile;
  TPtr<TOpenDialog> opendlg = new TOpenDialog(NULL);

  // See if the alias exists.  if not then add it.
  if (!Session->IsAlias("MASTSQL"))
  {
    DataFile = DataDirectory() + "MASTSQL.GDB";
    if (!FileExists(DataFile))
    {
      opendlg->Filter = "Interbase files (*.gdb)|*.GDB";
      if (opendlg->Execute())
        DataFile = opendlg->FileName;
    }
    else
      MessageBox(NULL,"Cannot locate Interbase data file: MASTSQL.GDB","Warning!",MB_OK);
    try
    {
      Params->Values["SERVER NAME"] = DataFile;
      Params->Values["USER NAME"] = "SYSDBA";
      Session->AddAlias("MASTSQL", "INTRBASE", Params);
    }
    catch(...)
    {
       Params->Free();
       return;
    }
    Params->Free();
  }
  SetDatabaseAlias("MASTSQL");
}

bool TMastData::DataSetApplyUpdates(TDBDataSet *DataSet, bool Apply)
{
  bool retval = true;
    if ((DataSet->State==dsEdit || DataSet->State==dsInsert ||DataSet->State==dsSetKey)
        || DataSet->UpdatesPending)
    {
      if (Apply)
      {
        Database->ApplyUpdates(OPENARRAY(TDBDataSet*, (DataSet)));
        DataSet->CancelUpdates();
      }
      else
      {
        if (Confirm("Unsaved changes, exit anyway?"))
          DataSet->CancelUpdates();
        else
          retval = false;
      }
    }
  return retval;
}


//---------------------------------------------------------------------------
void __fastcall TMastData::PartsBeforeOpen(TDataSet *DataSet)
{
  Vendors->Open();
}
//---------------------------------------------------------------------------
void __fastcall TMastData::PartsCalcFields(TDataSet *DataSet)
{
  PartsBackOrd->Value = (PartsOnOrder->Value > PartsOnHand->Value);
}
//---------------------------------------------------------------------------
void __fastcall TMastData::PartsQueryCalcFields(TDataSet *DataSet)
{
  PartsQueryBackOrd->Value = (PartsOnOrder->Value > PartsOnHand->Value);
}
//---------------------------------------------------------------------------
//If user cancels the updates to the orders table, cancel the updates to
//the line items as well
void __fastcall TMastData::OrdersAfterCancel(TDataSet *DataSet)
{
  Cust->CancelUpdates();
  Parts->CancelUpdates();
  Items->CancelUpdates();
  Orders->CancelUpdates();
}
//---------------------------------------------------------------------------
// Order Entry

//Post new LastInvoiceDate to CUST table.
void __fastcall TMastData::OrdersAfterPost(TDataSet *DataSet)
{
  Set<TLocateOption,0,1> flags;
  if (Cust->Locate("CustNo", OrdersCustNo->Value, flags) &&
    (CustLastInvoiceDate->Value < OrdersShipDate->Value))
  {
    Cust->Edit();
    CustLastInvoiceDate->Value = OrdersShipDate->Value;
    Cust->Post();
  }
  Database->ApplyUpdates(OPENARRAY(TDBDataSet*, (Orders, Items, Parts, Cust)));
}
//---------------------------------------------------------------------------
void __fastcall TMastData::OrdersBeforeCancel(TDataSet *DataSet)
{
  if( (Orders->State == dsInsert) &&  !(Items->Bof && Items->Eof) )
    if (!Confirm("Cancel order being inserted and delete all line items?"))
      Abort();
}
//---------------------------------------------------------------------------
void __fastcall TMastData::OrdersBeforeClose(TDataSet *DataSet)
{
  Items->Close();
  Emps->Close();
  CustByOrd->Close();
}

⌨️ 快捷键说明

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