Hi..
how can we (insert,update,delete and select) using linq with c# in visual studio 2008.
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Pravin MorePosted Nov 22, 2011, 2:16 AM
here is sample code for insert ,update ,delete.....try it
//Delete
public void DeleteProduct(int productId)
{
using (MyDataContentDataContext context = new MyDataContentDataContext())
{
Product currentProduct = context.Products.FirstOrDefault(p => p.ProductId == productId);
context.Products.DeleteOnSubmit(currentProduct);context.SubmitChanges();
}
//Insert
public void AddProduct()
{
Product product = new Product();
product.Name = "Product3";
product.Description="This is product 3 Description";
product.Price=10;
using (MyDataContentDataContext context = new MyDataContentDataContext())
{
context.Products.InsertOnSubmit(product);
context.SubmitChanges();
}
}
//Update
public void UpdateProduct(int productId, string name, string description, double price)
{
using (MyDataContentDataContext context = new MyDataContentDataContext())
{
Product currentProduct = context.Products.FirstOrDefault(p => p.ProductId == productId);
currentProduct.Name = name;
currentProduct.Description = description;
currentProduct.Price = price;
context.SubmitChanges();
}
}
Thanks,
Pravin.
Satyapriya NayakPosted Nov 22, 2011, 2:11 AM
Please refer the below link
http://www.dotnetbips.com/articles/56f8f29d-2617-4f99-a8b4-977703ebf780.aspx
Thanks