[Linq to Sql] Insert et delete
Bonjour !
Voilà que j'essaie de suivre les tutoriaux de la page suivante: http://msdn.microsoft.com/fr-fr/data/cc904318.aspx
Le soucis, lorsque je veux ajouter ou supprimer un produit:
Code:
1 2 3 4 5 6 7 8
|
Code SnippetNorthwindDataContext db = new NorthwindDataContext
var toyProducts = from p in db.Products where p.Category.CategoryName.Contains("toy") select p
db.Products.RemoveAll(toyProducts);
db.submitChanges(); |
Et mon code actuel (pour la suppression):
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
DataClasses1DataContext db = new DataClasses1DataContext();
//Rcupration de l'id
string id = this.gridStandard.CurrentRow.Cells["id"].Value.ToString();
var delClients = from p in db.xdb_clients where p.id.Contains(id) select p;
db.xdb_clients.removeAll(delClients); //RemoveAll n'existe pas...
try
{
db.SubmitChanges();
}
catch (Exception dex)
{
MessageBox.Show(dex.Message, "Erreurs", MessageBoxButtons.OK, MessageBoxIcon.Information);
//Gestion d'erreur dlinq, on peut faire mieux que mon msgbox?
}
finally
{
this.gridStandard.DataSource = delClients.ToList(); //Mise à jour
} |
Vous trouverez les problèmes dans le code, soit, removeAll et la gestion de l'erreur...
Ainsi que pour l'insert
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
NorthwindDataContext db = new NorthwindDataContext
Category category = new Category();
category.CategoryNAme = "Scott's Toys";
Product product1 = new Product();
product1.ProductName = "Toy 1";
Product product2 = new Product();
product2.productName = "Toy 2";
category.Products.add(product1);
category.Products.add(product2);
db.Categories.Add(category);
db.submitchanges(); |
Mon code actuellement:
Code:
1 2 3 4 5 6 7
|
Code SnippetDataClasses1DataContext db = new DataClasses1DataContext();
db.SubmitChanges(); |
\o/. Ici le problème, comme en haut d'ailleurs, c'est que je n'ai pas "Category" ou "Product" à quoi est-ce que sa correspond?
Je vois dans son tutoriel "[Version RTM] La méthode Add() de l'interface ITable est renommé en InsertOnSubmit().
Remplacer donc, dans l'exemple si-dessus, la ligne << db.Categories.Add(category) >> par << db.Categories.InsertOnSubmit(category)>>.
Bah je n'ai pas d'interface Itable (mais la méthode insertonsubmit existe, je l'ai dans mon intellisens).
Comment donc faire un delete et un insert en linq to sql?
Meilleures salutations !