Probleme d'insertion d'une clef primaire OleAdo
A la suite de comment creer une base vide Access sans Access, j'ai fais des recherches, et j'ai trouve comment creer une table, et inserrer des champs, seulement je n'arrive pas a inserrer un champ pour une clef primaire.
La Form contient un TButton un TADOCommand un TADOConnection
Le code:
Code:
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
|
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <ComObj.hpp>
#include "Ouvrir_Base_Vide.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
// ne pas oublier d'inclure #include <comobj.hpp>
Variant vADO;
AnsiString Filename = "C:\\Documents and Settings\\blondelle\\Mes documents\\BASES\\maBdd26.mdb";
if (FileExists(Filename.c_str()))
{
DeleteFile(Filename);
}
AnsiString provider = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +Filename+";";
vADO = Variant::CreateObject("ADOX.Catalog");
vADO.OleFunction("create", provider.c_str());
// on connecte ADOConnection
ADOConnection1->ConnectionString = provider;
ADOConnection1->LoginPrompt = false;
ADOCommand1->Connection = ADOConnection1;
// on cree une table vide au Nom Essai
AnsiString nom_table = "CREATE TABLE Essai (typename TEXT(50))";
ADOCommand1->CommandText = nom_table;
ADOCommand1->Execute();
// on cree les entetes de trois colonnes authorname, email, web dans une table au nom Authors
AnsiString nomTable_colonnes = "CREATE TABLE Authors (authorname TEXT(50),email TEXT(50),web TEXT(50))";
ADOCommand1->CommandText = nomTable_colonnes;
ADOCommand1->Execute();
// les clefs primaires ne se crees pas
// on cree une clef primaire dans la table Essai
AnsiString clef = "CREATE INDEX idxPrimary ON Essai (typename) WITH PRIMARY";
ADOCommand1->CommandText = clef;
ADOCommand1->Execute();
// on cree une clef primaire dans la table Authors
AnsiString clef1 = "CREATE INDEX idxPrimary ON Authors (authorname) WITH PRIMARY";
ADOCommand1->CommandText = clef1;
ADOCommand1->Execute();
// en quittant on libere les Variant
vADO = Unassigned;
} |