1.Introduction:
CRUD veut dire 4 chose:
* C=>Create ->Insert
* R =>REad ->Select
* U =>Update
* D-> Delete
2.Exemple de CRUD de la table Employee de la base "Sample" AdventureWork
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AWModel;
namespace SimpleQueries
{
class Program
{
static void Main(string[] args)
{
//recupereEtMiseAJourEmployee();
// recupereEtMiseAJourCustomer();
supprimeSalesDeCustomer();
}
/*VALABLE 100% */
private static void recupereEtMiseAJourEmployee()
{
using (var context = new AdventureWorksEntities())
{
var employe = context.Employee.First(c => c.EmployeeID == 5);
employe.Title = "Marketing Assistant";
context.SaveChanges();
}
}
private static void recupereEtMiseAJourCustomer()
{
using (var context = new AdventureWorksEntities())
{
var custom = context.Customer.First(c => c.CustomerID == 10);
//Relation Customer-SalesOrderHeader = 1..*
custom.SalesOrderHeader.Add(new SalesOrderHeader
{
OrderDate = DateTime.Now,
DueDate = DateTime.Now.AddMonths(1),
ModifiedDate = DateTime.Now,
Comment = "Don't forget to ship this!" //Abdennour vous Souhaite Le Bienvenue
});
context.SaveChanges();
}
}
private static void insertContactCascade()
{
using (var context = new AdventureWorksEntities())
{
var contct = new Contact { FirstName = "Abdennour", Title = "Mr.", LastName = "Toumi", EmailAddress = "abdennour.toumi@gmail.com",
EmailPromotion = 2, Phone = "334-555-0137",ModifiedDate=DateTime.Now };
//Relation Contact-ContactCreditCard = 1..*
contct.ContactCreditCard.Add(new ContactCreditCard{
//INserer Les valeurs de nouveau contact ContactCreditCard
});
context.SaveChanges();
}
}
/*VALABLE 100% */
private static void supprimeSalesDeCustomer()
{
using (var context = new AdventureWorksEntities())
{
var orders = from o in context.SalesOrderHeader where o.CustomerID == 50 select o;
foreach (var ord in orders)
{
context.SalesOrderHeader.DeleteObject(ord);
}
context.SaveChanges();
}
}
}
}
CRUD veut dire 4 chose:
* C=>Create ->Insert
* R =>REad ->Select
* U =>Update
* D-> Delete
2.Exemple de CRUD de la table Employee de la base "Sample" AdventureWork
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AWModel;
namespace SimpleQueries
{
class Program
{
static void Main(string[] args)
{
//recupereEtMiseAJourEmployee();
// recupereEtMiseAJourCustomer();
supprimeSalesDeCustomer();
}
/*VALABLE 100% */
private static void recupereEtMiseAJourEmployee()
{
using (var context = new AdventureWorksEntities())
{
var employe = context.Employee.First(c => c.EmployeeID == 5);
employe.Title = "Marketing Assistant";
context.SaveChanges();
}
}
private static void recupereEtMiseAJourCustomer()
{
using (var context = new AdventureWorksEntities())
{
var custom = context.Customer.First(c => c.CustomerID == 10);
//Relation Customer-SalesOrderHeader = 1..*
custom.SalesOrderHeader.Add(new SalesOrderHeader
{
OrderDate = DateTime.Now,
DueDate = DateTime.Now.AddMonths(1),
ModifiedDate = DateTime.Now,
Comment = "Don't forget to ship this!" //Abdennour vous Souhaite Le Bienvenue
});
context.SaveChanges();
}
}
private static void insertContactCascade()
{
using (var context = new AdventureWorksEntities())
{
var contct = new Contact { FirstName = "Abdennour", Title = "Mr.", LastName = "Toumi", EmailAddress = "abdennour.toumi@gmail.com",
EmailPromotion = 2, Phone = "334-555-0137",ModifiedDate=DateTime.Now };
//Relation Contact-ContactCreditCard = 1..*
contct.ContactCreditCard.Add(new ContactCreditCard{
//INserer Les valeurs de nouveau contact ContactCreditCard
});
context.SaveChanges();
}
}
/*VALABLE 100% */
private static void supprimeSalesDeCustomer()
{
using (var context = new AdventureWorksEntities())
{
var orders = from o in context.SalesOrderHeader where o.CustomerID == 50 select o;
foreach (var ord in orders)
{
context.SalesOrderHeader.DeleteObject(ord);
}
context.SaveChanges();
}
}
}
}
No comments:
Post a Comment