Enterprise Library –
récupérer des valeurs en retour de Procédures stockées
1 – solution : retourner un paramètre contenant une valeur (exemple l’identifiant d’une clé auto incrémentée)
|
CREATE PROCEDURE [dbo].[InsertContact]
(
@ContactName char(100),
@ContactFirstName char(100),
@ContactAge int,@ContactCategoryID int
)
AS
declare @ContactID int;
INSERT INTO [Contact]([ContactName]
,[ContactFirstName] ,[ContactAge] ,[ContactCategoryID] )
VALUES(@ContactName,@ContactFirstName,@ContactAge,@ContactCategoryID)
SET @ContactID= SCOPE_IDENTITY();
return @ContactID;
|
Depuis enterprise Library
A-Création de la commande
|
public DbCommand
ConstructInsertCommand(Database db,Contact Contact)
{
DbCommand command = db.GetStoredProcCommand("dbo.AddContact");
db.AddInParameter(command,
"ContactAge",DbType.Int32,Contact.ContactAge);
db.AddInParameter(command,
"ContactCategoryID",DbType.Int32,Contact.ContactCategoryID);
db.AddInParameter(command,
"ContactFirstName",DbType.String,Contact.ContactFirstName);
db.AddInParameter(command,
"ContactName",DbType.String,Contact.ContactName);
db.AddParameter(command, "ContactID", DbType.Int32,ParameterDirection.ReturnValue,"ContactID",DataRowVersion.Default,null);
return command;
}
|
B – éxecution de la commande puis récupération du nouvel identifiant
|
public int Add(IInsertFactory<TDomainObject> insertFactory,TDomainObject domainObj)
{
using (DbCommand command = insertFactory.ConstructInsertCommand(db, domainObj))
{
int result
= db.ExecuteNonQuery(command);
insertFactory.SetNewID(db, command,
domainObj);
return
result;
}
}
public void SetNewID(Database db, DbCommand command, Contact
domainObj)
{
domainObj.ContactID = (int)db.GetParameterValue(command, "ContactID");
}
|
2- Autre solution : passer un paramètre OUT à la procédure stockée
|
CREATE PROCEDURE [dbo].[InsertContact]
(
@ContactID int OUT,
@ContactName char(100),
@ContactFirstName char(100),
@ContactAge int,@ContactCategoryID int
)
AS
INSERT INTO [Contact]([ContactName]
,[ContactFirstName] ,[ContactAge] ,[ContactCategoryID] )
VALUES(@ContactName,@ContactFirstName,@ContactAge,@ContactCategoryID);
/* affectation à la variable du nouvel identifiant (la column ContactID etant auto incrémentée */
SET @ContactID= SCOPE_IDENTITY();
|
Depuis enterprise Library
A-Création de la commande
|
public DbCommand
ConstructInsertCommand(Database db,Contact Contact)
{
DbCommand command = db.GetStoredProcCommand("dbo.AddContact");
db.AddInParameter(command,
"ContactAge",DbType.Int32,Contact.ContactAge);
db.AddInParameter(command,
"ContactCategoryID",DbType.Int32,Contact.ContactCategoryID);
db.AddInParameter(command,
"ContactFirstName",DbType.String,Contact.ContactFirstName);
db.AddInParameter(command, "ContactName",DbType.String,Contact.ContactName);
db.AddOutParameter(command, "ContactID", DbType.Int32, 4);
return command;
}
|
B – éxecution de la commande puis récupération du nouvel identifiant
|
public int Add(IInsertFactory<TDomainObject> insertFactory,TDomainObject domainObj)
{
using (DbCommand command = insertFactory.ConstructInsertCommand(db, domainObj))
{
int result
= db.ExecuteNonQuery(command);
insertFactory.SetNewID(db, command,
domainObj);
return
result;
}
}
public void SetNewID(Database db, DbCommand command, Contact
domainObj)
{
domainObj.ContactID = (int)db.GetParameterValue(command, "ContactID");
}
|
La seconde soltuion est peut être la mieux , en tout cas la plus utilisée
Note dans la méthode SetNewID j'affecte le nouvel identifiant,à mon objet métier (ici un contact) c'est à dire donc la valeur récupérée aprés execution de la procédure stockée
Note dans la méthode SetNewID j'affecte le nouvel identifiant,à mon objet métier (ici un contact) c'est à dire donc la valeur récupérée aprés execution de la procédure stockée
