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

📄 ch14.htm

📁 好书《C++ Builder高级编程技术》
💻 HTM
📖 第 1 页 / 共 5 页
字号:
  {

    case 1:

      
DMod->NamesTable->Insert();

      break;

    case 2:

      DMod->AddressTable->Insert();

      break;

    case 3:

      DMod->PhoneTable->Insert();

      break;

    case 4:

      DMod->EMailTable->Insert();

      
break;

  }

}

void __fastcall TForm1::Cancel1Click(TObject *Sender)

{

  AnsiString S;

  switch (dynamic_cast<TComponent&>(*PageControl1->ActivePage).Tag)

  {

    case 1:

      DMod->NamesTable->Cancel();

      break;

    
case 2:

      DMod->AddressTable->Cancel();

      break;

    case 3:

      DMod->PhoneTable->Cancel();

      break;

    case 4:

      DMod->EMailTable->Cancel();

      break;

    default:

      ShowMessage(GetError(1, S));

  
}

}

void __fastcall TForm1::Delete1Click(TObject *Sender)

{

  AnsiString S;

  AnsiString Msg("Are you sure you want to delete \r %s?");

  Set<TMsgDlgBtn, 0, 8> Btns;

  Btns << mbYes << mbNo;

  switch 
(dynamic_cast<TComponent&>(*PageControl1->ActivePage).Tag)

  {

    case 1:

      Msg = Format(Msg, OPENARRAY(TVarRec,

        (DMod->NamesTableFirstLastCompany->AsString)));

      if (MessageDlg(Msg, mtInformation, Btns, 0) == 
ID_YES)

        DMod->CascadingDelete();

      break;

    case 2:

      Msg = Format(Msg, OPENARRAY(TVarRec, (DMod->Address)));

      if (MessageDlg(Msg, mtInformation, Btns, 0) == ID_YES)

        DMod->AddressTable->Delete();

      
break;

    case 3:

      Msg = Format(Msg, OPENARRAY(TVarRec, (DMod->Phone)));

      if (MessageDlg(Msg, mtInformation, Btns, 0) == ID_YES)

        DMod->PhoneTable->Delete();

      break;

    case 4:

      Msg = Format(Msg, 
OPENARRAY(TVarRec, (DMod->EMail)));

      if (MessageDlg(Msg, mtInformation, Btns, 0) == ID_YES)

        DMod->EMailTable->Delete();

      break;



    default:

      ShowMessage(GetError(1, S));

  }

}

void __fastcall 
TForm1::SearchClick(TObject *Sender)

{

  AnsiString S, IndexName;

  if (InputQuery("Search for Name", "Enter Name: ", S))

  {

    IndexName = DMod->NamesTable->IndexName;

    SetupIndex(Sender);

    
DMod->NamesTable->FindNearest(OPENARRAY(TVarRec, (S)));

    DMod->NamesTable->IndexName = IndexName;

  }

}

void __fastcall TForm1::About1Click(TObject *Sender)

{

  AboutBox->ShowModal();

}

void __fastcall 
TForm1::PostBtnClick(TObject *Sender)

{

  DMod->PostAll();

}

</FONT></PRE>
<P><A NAME="Heading12"></A><FONT COLOR="#000077"><B>Listing 14.3. The header for
the data module.</B></FONT></P>
<PRE><FONT 
COLOR="#0066FF">///////////////////////////////////////

// File: DMod1.h

// Project: KdAdd

// Copyright (c) 1997 by Charlie Calvert

//

#ifndef DMod1H

#define DMod1H

#include &lt;Forms.hpp&gt;

#include &lt;DBTables.hpp&gt;

#include 
&lt;DB.hpp&gt;

#include &lt;Classes.hpp&gt;

class TDMod : public TDataModule

{

__published:

  TTable *NamesTable;

  TDataSource *dsNames;

  TTable *AddressTable;

  TTable *PhoneTable;

  TTable *MemoTable;

  TDataSource *dsAddress;

  
TDataSource *dsPhone;

  TDataSource *dsMemo;

  TAutoIncField *PhoneTablePhoneCode;

  TStringField *PhoneTableDescription;

  TStringField *PhoneTableNumber;

  TStringField *PhoneTableExt;

  TIntegerField *PhoneTableNameCode;

  TTable 
*EMailTable;

  TDataSource *dsEmail;

  TAutoIncField *EMailTableEMailCode;

  TStringField *EMailTableAddress;

  TStringField *EMailTableDescription;

  TStringField *EMailTableService;

  TIntegerField *EMailTableNameCode;

  TQuery 
*EMailDeleteQuery;

  TQuery *MemoDeleteQuery;

  TQuery *PhoneDeleteQuery;

  TQuery *AddressDeleteQuery;

  TQuery *NamesDeleteQuery;

  TAutoIncField *MemoTableMemoCode;

  TStringField *MemoTableDescription;

  TMemoField *MemoTableMemoData;

  
TIntegerField *MemoTableNameCode;

  TAutoIncField *NamesTableNameCode;

  TStringField *NamesTableFirstName;

  TStringField *NamesTableLastName;

  TStringField *NamesTableCompany;

  TAutoIncField *AddressTableAddCode;

  TStringField 
*AddressTableAddress1;

  TStringField *AddressTableAddress2;

  TStringField *AddressTableCity;

  TStringField *AddressTableState;

  TStringField *AddressTableZip;

  TIntegerField *AddressTableNameCode;

  TStringField *NamesTableFirstLastCompany;

  
void __fastcall NamesTableCalcFields(TDataSet *DataSet);

private:

  AnsiString __fastcall GetAddress();

  AnsiString __fastcall GetPhone();

  AnsiString __fastcall GetEMail();

public:

  virtual __fastcall TDMod(TComponent* Owner);

  void 
PostAll(void);

  void CascadingDelete(void);

  __property AnsiString Address={read=GetAddress};

  __property AnsiString Phone={read=GetPhone};

  __property AnsiString EMail={read=GetEMail};

};

extern TDMod *DMod;

</FONT></PRE>
<PRE><FONT 
COLOR="#0066FF">#endif

</FONT></PRE>
<P><A NAME="Heading13"></A><FONT COLOR="#000077"><B>Listing 14.4. The code for the
data module.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">///////////////////////////////////////

// File: DMod1.cpp

// Project: 
KdAdd

// Copyright (c) 1997 by Charlie Calvert

//

#include &lt;vcl.h&gt;

#pragma hdrstop

#include &quot;DMod1.h&quot;

#pragma resource &quot;*.dfm&quot;

TDMod *DMod;

__fastcall TDMod::TDMod(TComponent* Owner)

  : TDataModule(Owner)

{

  
NamesTable-&gt;Open();

  AddressTable-&gt;Open();

  PhoneTable-&gt;Open();

  MemoTable-&gt;Open();

  EMailTable-&gt;Open();

}

void __fastcall TDMod::NamesTableCalcFields(TDataSet *DataSet)

{

  AnsiString Temp = NamesTableFirstName-&gt;Value+ 
&quot; &quot; +NamesTableLastName-&gt;Value;

  if (Temp == &quot; &quot;)

    NamesTableFirstLastCompany-&gt;Value = NamesTableCompany-&gt;Value;

  else

    NamesTableFirstLastCompany-&gt;Value = Temp;

}

void DoPost(TDataSet *Data)

{

  if 
((Data-&gt;State == dsInsert) || (Data-&gt;State == dsEdit))

    Data-&gt;Post();

}

void TDMod::PostAll(void)

{

  int i;

  for (i = 0; i &lt; ComponentCount; i++)

    if (dynamic_cast&lt;TTable*&gt;(Components[i]))

      
DoPost((TDataSet*)(Components[i]));

}

void TDMod::CascadingDelete(void)

{

  EMailDeleteQuery-&gt;ParamByName(&quot;NameCode&quot;)-&gt;AsInteger =

    EMailTableNameCode-&gt;Value;

  EMailDeleteQuery-&gt;ExecSQL();

  
MemoDeleteQuery-&gt;ParamByName(&quot;NameCode&quot;)-&gt;AsInteger =

    MemoTableNameCode-&gt;Value;

  MemoDeleteQuery-&gt;ExecSQL();

  PhoneDeleteQuery-&gt;ParamByName(&quot;NameCode&quot;)-&gt;AsInteger =

    PhoneTableNameCode-&gt;Value;

  
PhoneDeleteQuery-&gt;ExecSQL();

  AddressDeleteQuery-&gt;ParamByName(&quot;NameCode&quot;)-&gt;AsInteger =

    AddressTableNameCode-&gt;Value;

  AddressDeleteQuery-&gt;ExecSQL();

  
NamesDeleteQuery-&gt;ParamByName(&quot;NameCode&quot;)-&gt;AsInteger =

    NamesTableNameCode-&gt;Value;

  NamesDeleteQuery-&gt;ExecSQL();

  NamesTable-&gt;Refresh();

}

⌨️ 快捷键说明

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