Cycle de progression Faire (quelque chose qui marche) -> comprendre ce que l’on fait/comment cela marche -> pousser plus loin les notions
| 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; |
| 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; } |
| 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"); } |
| 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(); |
| 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; } |
| 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"); } |