moi j'utilise ADO :
//Query.h
#define DivX_DATABASE "DivX.mdb"
// Le provider Microsoft.JET
#define IDS_STRCONNECTION "Provider=Microsoft.JET.OLEDB.4.0;Data source = "
// *******************************************************
// -----------------------------------
// Requete : QUERY_ALLDIVX
// Role : Selection de tous les DivX
// -----------------------------------
#define QUERY_ALLDIVX "SELECT * FROM DIVX_TABLE ORDER BY Title"
// -----------------------------------
// Requete : QUERY_ADDDIVX
// Role : Selection d' un emplacement vide dans la table des jours fériés
// -----------------------------------
#define QUERY_ADDDIVX "SELECT * FROM DIVX_TABLE WHERE ID IS NULL"
// -----------------------------------
// Requete : QUERY_DIVXBYID
// Role : Selection selon l' ID
// -----------------------------------
#define QUERY_DIVXBYID "SELECT * FROM DIVX_TABLE where ID = %d"
// -----------------------------------
//ConnectID.cpp
#include "Query.h" // Requetes
// Construction/Destruction
CConnectID::CConnectID()
{
// Initialisation de COM
CoInitialize(NULL);
// Creation
pConn.CreateInstance(__uuidof(Connection));
}
CConnectID::~CConnectID()
{
pConn.Release();
pConn = NULL;
// Desinitialisation de COM
CoUninitialize();
}
// METHODES ***********************************************
_RecordsetPtr CConnectID::GetDivXByID(int ID)
{
// Objets Recordset
_RecordsetPtr rec(__uuidof(Recordset));
// Formatage de la requete
str_Query.Format(QUERY_DIVXBYID,ID);
// -----------------------------
// Commande
try{
// Execution de la commande
CComVariant v (0L);
rec = pConn->Execute(str_Query.operator LPCTSTR(), &v, adOptionUnspecified);
}
// -----------------------------
// Traitement des erreurs
catch(_com_error &err){
// Affichage du message d' erreur
CATCH_COM_ERROR(err);
}
// Reccuperation du recordset
return rec;
}
_RecordsetPtr CConnectID::GetAllDivX()
{
// Objets Recordset
_RecordsetPtr rec(__uuidof(Recordset));
// Formatage de la requete
str_Query = QUERY_ALLDIVX;
// -----------------------------
// Commande
try{
// Execution de la commande
CComVariant v (0L);
rec = pConn->Execute(str_Query.operator LPCTSTR(), &v, adOptionUnspecified);
}
// -----------------------------
// Traitement des erreurs
catch(_com_error &err){
// Affichage du message d' erreur
CATCH_COM_ERROR(err);
}
// Reccuperation du recordset
return rec;
}
// *******************************************************
// Catch ERREURS
// *******************************************************
void CConnectID::CATCH_COM_ERROR(_com_error &err)
{
CString str_Message;
_bstr_t bstrSource(err.Source());
_bstr_t bstrDescription(err.Description());
// Formattage du message d' erreur
str_Message.Format("CConnectID Class\n\nCode = %08lx\nNom = %s\nSource = %s\nDétails = %s\n"
, err.Error()
, err.ErrorMessage()
, (LPCSTR)bstrSource
, (LPCSTR)bstrDescription );
// Affichage du message d' erreur
MessageBox(NULL,str_Message,"Erreur",MB_OK|MB_ICONINFORMATION);
}
void CConnectID::ConnectToDB()
{
// Chaine de Connexion
CString tmp;
tmp = IDS_STRCONNECTION;
str_Connection.Format("%s%s%s" ,tmp
,DivX_DATABASE
,";" );
// -----------------------------
// Tentative de connexion
try{
// Connection à la source de données
pConn->Open( _bstr_t(str_Connection),"","",NULL);
}
// -----------------------------
// Traitement des erreurs
catch(_com_error &err){
// Affichage du message d' erreur
CATCH_COM_ERROR(err);
}
}
void CConnectID::DisconnectToDB()
{
// Deconnection
pConn->Close();
}
void CConnectID::AddDivX( BSTR Title,
BSTR Date,
int Length,
int Vid,
int Snd,
int Size,
int Type,
int Number,
BSTR Language,
BSTR Codec)
{
// Objets Recordset
_RecordsetPtr rec(__uuidof(Recordset));
str_Query = QUERY_ADDDIVX;
CComVariant v (0L);
v.vt = VT_ERROR;
v.scode = DISP_E_PARAMNOTFOUND;
// -----------------------------
// Commande
try{
rec->PutRefActiveConnection (pConn);
HRESULT hr = rec->Open(str_Query.operator LPCTSTR(), v, adOpenForwardOnly, adLockOptimistic, adCmdText);
if (SUCCEEDED (hr)){
COleSafeArray fieldlist, valuelist;
long arrayindex[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
fieldlist.CreateOneDim(VT_VARIANT, 10);
valuelist.CreateOneDim (VT_VARIANT, 10);
// Liste des champs
CComVariant f1("Title" );
CComVariant f2("Date" );
CComVariant f3("Length" );
CComVariant f4("Vid_kbit_s" );
CComVariant f5("Snd_kbit_s" );
CComVariant f6("Size_Data" );
CComVariant f7("CD_Type" );
CComVariant f8("Number_CD" );
CComVariant f9("Language" );
CComVariant f10("Codec" );
fieldlist.PutElement(&arrayindex[0], &f1);
fieldlist.PutElement(&arrayindex[1], &f2);
fieldlist.PutElement(&arrayindex[2], &f3);
fieldlist.PutElement(&arrayindex[3], &f4);
fieldlist.PutElement(&arrayindex[4], &f5);
fieldlist.PutElement(&arrayindex[5], &f6);
fieldlist.PutElement(&arrayindex[6], &f7);
fieldlist.PutElement(&arrayindex[7], &f8);
fieldlist.PutElement(&arrayindex[8], &f9);
fieldlist.PutElement(&arrayindex[9], &f10);
// Liste des valeurs
CComVariant v1 (Title);
CComVariant v2 (Date);
CComVariant v3 (Length);
CComVariant v4 (Vid);
CComVariant v5 (Snd);
CComVariant v6 (Size);
CComVariant v7 (Type);
CComVariant v8 (Length);
CComVariant v9 (Number);
CComVariant v10(Codec);
valuelist.PutElement(&arrayindex[0], &v1);
valuelist.PutElement(&arrayindex[1], &v2);
valuelist.PutElement(&arrayindex[2], &v3);
valuelist.PutElement(&arrayindex[3], &v4);
valuelist.PutElement(&arrayindex[4], &v5);
valuelist.PutElement(&arrayindex[5], &v6);
valuelist.PutElement(&arrayindex[6], &v7);
valuelist.PutElement(&arrayindex[7], &v8);
valuelist.PutElement(&arrayindex[8], &v9);
valuelist.PutElement(&arrayindex[9], &v10);
rec->AddNew(fieldlist, valuelist) ;
rec->Close() ;
}
}
// -----------------------------
// Traitement des erreurs
catch(_com_error &err){
// Affichage du message d' erreur
CATCH_COM_ERROR(err);
}
}
void CConnectID::DelDivX(int ID)
{
// Objets Recordset
_RecordsetPtr rec(__uuidof(Recordset));
// Formatage de la requete
str_Query.Format(QUERY_DIVXBYID,ID);
CComVariant v (0L);
v.vt = VT_ERROR;
v.scode = DISP_E_PARAMNOTFOUND;
// -----------------------------
// Commande
try{
rec->PutRefActiveConnection(pConn);
HRESULT hr = rec->Open(str_Query.operator LPCTSTR(), v,adOpenForwardOnly, adLockOptimistic, adCmdText);
if (!rec->GetEndOfFile()){
rec->Delete(adAffectCurrent);
rec->Close();
}
}
// -----------------------------
// Traitement des erreurs
catch(_com_error &err){
// Affichage du message d' erreur
CATCH_COM_ERROR(err);
}
}
void CConnectID::UpdateDivX(int ID,
BSTR Title,
BSTR Date,
int Length,
int Vid,
int Snd,
int Size,
int Type,
int Number,
BSTR Language,
BSTR Codec)
{
// Objets Recordset
_RecordsetPtr rec(__uuidof(Recordset));
str_Query.Format(QUERY_DIVXBYID,ID);
CComVariant v (0L);
v.vt = VT_ERROR;
v.scode = DISP_E_PARAMNOTFOUND;
// -----------------------------
// Commande
try{
rec->PutRefActiveConnection (pConn);
HRESULT hr = rec->Open(str_Query.operator LPCTSTR(), v, adOpenForwardOnly, adLockOptimistic, adCmdText);
if (SUCCEEDED (hr)){
if (!rec->GetEndOfFile()){
CComVariant f1(ID);
CComVariant f2(Title);
CComVariant f3(Date);
CComVariant f4(Length);
CComVariant f5(Vid);
CComVariant f6(Snd);
CComVariant f7(Size);
CComVariant f8(Type);
CComVariant f9(Number);
CComVariant f10(Language);
CComVariant f11(Codec);
rec->PutCollect(L"ID", &f1);
rec->PutCollect(L"Title",&f2);
rec->PutCollect(L"Date",&f3);
rec->PutCollect(L"Length",&f4);
rec->PutCollect(L"Vid_kbit_s",&f5);
rec->PutCollect(L"Snd_kbit_s",&f6);
rec->PutCollect(L"Size_Data",&f7);
rec->PutCollect(L"CD_Type",&f8);
rec->PutCollect(L"Number_CD",&f9);
rec->PutCollect(L"Language",&f10);
rec->PutCollect(L"Codec",&f11);
rec->Update(v, v);
rec->Close();
}
}
}
// -----------------------------
// Traitement des erreurs
catch(_com_error &err){
// Affichage du message d' erreur
CATCH_COM_ERROR(err);
}
}