1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::cmdCreateClick(TObject *Sender)
{
AnsiString name=FileName->Text;
TADOConnection *connection=OpenConnection(name);
if (connection) {
// Créer la table et ses champs
ExecuteSQL(connection,
" CREATE TABLE tblClient ("
" uniqueKey INT PRIMARY KEY,"
" name VARCHAR(50),"
" description MEMO"
" );"
); // EDIT
// Et créer un premier client
ExecuteSQL(connection,
" INSERT INTO tblClient"
" (uniqueKey,name,description)"
" VALUES (1,\"mon client\",\"Mon tout premier client\");"); //EDIT
}
connection->Close();
delete connection;
}
//---------------------------------------------------------------------------
TADOConnection* TForm1::OpenConnection(AnsiString name) const
{
// Create the connections
TADOConnection* ado=new TADOConnection(NULL);
if (ado==NULL) throw EDatabaseError("Impossible de creer la connexion");
// Open the connection
ado->ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+name+";"; // EDIT
ado->LoginPrompt=false;
try {
ado->Open(); // Try to open the connection
}
catch(Exception &err) {
throw EDatabaseError(err.Message); // Convert error in EDatabaseError
}
return ado;
}
//------------------------------------------------------------------------
void __fastcall TForm1::cmdReadClick(TObject *Sender)
{
AnsiString name=FileName->Text;
TADOConnection* connection=OpenConnection(name);
if (connection) {
// Lit les données
TADOQuery *query=new TADOQuery(NULL);
try {
query->Connection=connection; /// Edit
query->SQL->Clear();
query->SQL->Text="SELECT * FROM tblClient;";
query->Open();
query->First();
while (!query->Eof) {
// Lit les données de l'enregistrement actuel
int key=query->FieldByName("uniqueKey")->AsInteger; // EDIT
txtKey->Text=AnsiString(key);
AnsiString nom=query->FieldByName("name")->AsString;
txtNom->Text=nom;
AnsiString description=query->FieldByName("description")->AsString;
txtDescription->Text=description;
// Continue avec le prochain record
query->Next();
}
}
__finally {
query->Close();
delete query;
}
}
connection->Close();
delete connection;
}
//---------------------------------------------------------------------------
void TForm1::ExecuteSQL(TADOConnection *connect,AnsiString SQLCommand) {
if ((connect==NULL)||(connect->Connected==false)) return;
TADOQuery *query=new TADOQuery(NULL);
try {
query->Connection=connect; // EDIT
query->SQL->Clear();
query->SQL->Text=SQLCommand;
query->ExecSQL();
query->Close();
}
__finally {
delete query;
}
}
//--------------------------------------------------------------------------- |
Partager