bjr
je doi realiser une connection a une base de donness.
etan donne que j avais tres peu de temps
j ai repris une source dun programmeur
elle va correctement rechercher les donne dans une requette pour les sauvegarder ds un fichier texte
mais je ne vois pas commen ne pas la mettre au format xlm
qqun aurai la reponse?
je compte m y mettre tte la nuit sur ado pour faire un satane projet..j ai pas le temps de m y prendre avec les odbc de bla bla bla..
merci d avance
si joint le code
bravo au developeur d ado..en 5 min on comprend tt!!!
#import "c:\Program Files\Common Files\system\ado\msado15.dll" \
no_namespace rename("EOF", "EndOfFile")
#include <ole2.h>
#include <stdio.h>
#include <conio.h>
#include <io.h>
//Function declarations
inline void TESTHR(HRESULT x) {if FAILED(x) _com_issue_error(x);};
bool FileExists(void);
void SaveX1(void);
void PrintProviderError(_ConnectionPtr pConnection);
void PrintComError(_com_error &e);
// Main Function
void main()
{
if(FAILED(::CoInitialize(NULL)))
return;
//If File exists in the specified directory, then display error
if (!FileExists())
{
SaveX1();
}
::CoUninitialize();
}
// SaveX1 Function
//First, access and save the LIST table.
void SaveX1()
{
HRESULT hr = S_OK;
// Define ADO object pointers.
// Initialize pointers on define.
// These are in the ADODB:: namespace.
_RecordsetPtr pRstList = NULL;
//Definitions of other variables
_bstr_t strCnn("Provider=Microsoft.JET.OLEDB.4.0;"
"Data source = H:\\DATA.mdb;");
try
{
TESTHR(pRstList.CreateInstance(__uuidof(Recordset)));
pRstList->Open("SELECT * FROM LIST",strCnn,
adOpenDynamic,adLockBatchOptimistic,adCmdText);
// For sake of illustration, save the Recordset to a diskette
// in txt format.
pRstList->Save("H:\\LIST.txt",adPersistXML);
pRstList->Close();
}
catch(_com_error &e)
{
// Notify the user of errors if any.
// Pass a connection pointer accessed from the Recordset.
_variant_t vtConnect = pRstList->GetActiveConnection();
// GetActiveConnection returns connect string if connection
// is not open, else returns Connection object.
switch(vtConnect.vt)
{
case VT_BSTR:
PrintComError(e);
break;
case VT_DISPATCH:
PrintProviderError(vtConnect);
break;
default:
printf("Errors occured.");
break;
}
}
}
// PrintProviderError Function
void PrintProviderError(_ConnectionPtr pConnection)
{
// Print Provider Errors from Connection object.
// pErr is a record object in the Connection's Error collection.
ErrorPtr pErr = NULL;
if( (pConnection->Errors->Count) > 0)
{
long nCount = pConnection->Errors->Count;
// Collection ranges from 0 to nCount -1.
for(long i = 0;i < nCount;i++)
{
pErr = pConnection->Errors->GetItem(i);
printf("Error number: %x\t%s\n", pErr->Number,
(LPCSTR) pErr->Description);
}
}
}
// PrintComError Function
void PrintComError(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
// Print COM errors.
printf("Error\n");
printf("\tCode = %08lx\n", e.Error());
printf("\tCode meaning = %s\n", e.ErrorMessage());
printf("\tSource = %s\n", (LPCSTR) bstrSource);
printf("\tDescription = %s\n", (LPCSTR) bstrDescription);
}
bool FileExists()
{
struct _finddata_t txt_file;
long hFile;
if( (hFile = _findfirst("H:\\LIST.txt", &txt_file )) != -1L)
{
printf( "File already exists!\n" );
return(true);
}
else
return (false);
}