Übungen Visual-CPP Datenbanken

Bsp Mathematiker

NameText 20
LebensdatenText 30
NameLebensdaten
Pythagoras570 - 510
Euklid365 - 300
Al-Chwarizmi780 - 835
Pascal1623 - 1662
Euler1707 - 1783
Gauß1777 - 1855

#include <vcl.h>
#pragma hdrstop
 
#include "DB1Unit1.h"
#include "Unit2.h"
 
 
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{ DBNavigator1->Visible="true";
  DBGrid1->Visible="true";
}
//---------------------------------------------------------------------------
 
void show(TDataSet* t)
{t->First();
 while (!t->Eof) {
   AnsiString s = t->FieldByName("Name")->AsString+";"+t->FieldByName("Lebensdaten")->AsString;
   Form1->ListBox1->Items->Add(s);
   t->Next();
 }
}
 
void __fastcall TForm1::Button2Click(TObject *Sender)
{show(FDTable1);
}
//---------------------------------------------------------------------------
 
void __fastcall TForm1::Button3Click(TObject *Sender)
{ ListBox1->Sorted=true;
  if (SaveDialog1->Execute())
	  ListBox1->Items->SaveToFile(SaveDialog1->FileName);
}
//---------------------------------------------------------------------------
 
void __fastcall TForm1::Button4Click(TObject *Sender)
{ Form2->ShowModal();
}
//---------------------------------------------------------------------------
void Suche(TDataSet *t,AnsiString inhalt)
{
   bool gefunden=false;
   t->First();
   while (!t->Eof)
   {
		AnsiString s = t->FieldByName("Name")->AsString;
		if(inhalt==s)
		{
		   ShowMessage(t->FieldByName("Name")->AsString+"  "+t->FieldByName("Lebensdaten")->AsString);
           gefunden=true;
        }
		t->Next();
   }
   if(!gefunden)ShowMessage("Es konnte kein Datensatz gefunden werden!");
}
 
 
void __fastcall TForm1::Button5Click(TObject *Sender)
{
   AnsiString suche=InputBox("Suche","Name eingeben","");
   Suche(FDTable1, suche);
}
//---------------------------------------------------------------------------

#include "DB1Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void Eingabe(TDataSet* t)
{t->Append();   // Leerer Datensatz wird an die Tabelle angehängt
 t->FieldByName("Name")->AsAnsiString = Form2->Edit1->Text;
 t->FieldByName("Lebensdaten")->AsAnsiString = Form2->Edit2->Text;
}



void __fastcall TForm2::Button1Click(TObject *Sender)
{Eingabe(Form1->FDTable1);
 Form2->Close();
}