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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Pravin MoreRank #1,419 · 1.2K reputationPosted Nov 21, 2011, 9:16 PM EST
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 NayakRank #47 · 31.5K reputationPosted Nov 21, 2011, 9:11 PM EST
Please refer the below link
http://www.dotnetbips.com/articles/56f8f29d-2617-4f99-a8b4-977703ebf780.aspx
Thanks