[DELPHI pour .NET 2.0] Comment faire des requêtes paramétrées ?
Bonjour,
Vu que j'ai eu pas mal de difficultés à trouver des exemples de requêtes paramétrées en utilisant AdoDbxClient, je mets ma petite contribution ici.
Vous créez un nouveau projet ASP.NET, vous ajoutez la référence Borland.Data.AdoDbxClient et vous ajoutez un Label nommé Label1 à votre page Default.aspx.
Mettez ceci dans votre Page_Load :
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
| uses SysUtils;
[Code managé du concepteur]
procedure TDefault.Page_Load(sender: System.Object; e: System.EventArgs);
var
TAdoDbxConnection1 : Borland.Data.AdoDbxClientProvider.TAdoDbxConnection;
TAdoDbxCommand1 : Borland.Data.AdoDbxClientProvider.TAdoDbxCommand;
DBReader1 : System.data.common.DbDataReader;
bdparam: DbParameter;
begin
try
TAdoDbxConnection1 := Borland.Data.AdoDbxClientProvider.TAdoDbxConnection.Create;
TAdoDbxConnection1.ConnectionString := 'ma chaine de connexion';
TAdoDbxConnection1.Open;
TAdoDbxCommand1 := Borland.Data.AdoDbxClientProvider.TAdoDbxCommand.Create;
TAdoDbxCommand1.Connection := TAdoDbxConnection1;
//NB : vous pouvez faire TAdoDbxConnection1.CreateCommand si vous voulez utiliser un DbCommand au lieu d'un TAdoDbxCommand.
TAdoDbxCommand1.CommandText := 'SELECT e.NOM '+
'FROM EMPLOYES e '+
'WHERE e.ID = ?';
//ajout d'un paramètre
//NB : paramètres nommés ne sont pas encore disponibles
bdparam:= TAdoDbxCommand1.CreateParameter;
bdparam.DbType:= DbType.Int32;
bdparam.Value:= System.Object(1);
TAdoDbxCommand1.Parameters.Add(bdparam);//ID
//préparation + exécution requête
TAdoDbxCommand1.Prepare;
DBReader1 := TAdoDbxCommand1.ExecuteReader;
//affichage du premier nom trouvé dans Label1
if DBReader1.Read then
Label1.Text := DBReader1['NOM'].Tostring
else
Label1.Text:= 'pas de données';
finally
FreeAndNil(bdparam);
FreeAndNil(TAdoDbxCommand1);
TAdoDbxConnection1.Close;
end;
end; |