Goal: In C#, to take a struct and insert data to a sql table named Product_Master
via linq to sql - Or any other solution that is most efficient.
I am using a web service that returns the r开发者_StackOverflow中文版esponse in the form of a struct (product_ID, DateTime, Description)
. The name of the struct is productOutput
. What might be the most efficient way to accomplish this. In another part of the project I have been using linq to sql to query the table so in trying to keep consistency I have been trying to use linq, without success. I am interested in the best way so if linq isn’t the best way I am open to any solution. Note: I am new to c# so the more details the better.
In LINQ2SQL you first need to add your table to the LINQ/DBML designer, then you can use it in code like this
using (var context = new MyDataContext())
{
var pm = new Product_Master(); // Type generated by the LINQ designer.
pm.Property1 = "foo";
pm.Property2 = 123;
//...
context.Product_Master.InsertOnSubmit(pm);
context.SubmitChanges();
}
You need to map your fields from the struct manually to the properties of the Product_Master
object.
There might be issues with structs being passed since LINQ normally updates the property in your object that corresponds to the identity column in your table. This might not work with a struct object being passed by value. I've never tried it but I don't think I'd even try just because of the potential complications.
精彩评论