views:

160

answers:

1

Hi SO,

I'm trying to get an insert to work using LINQ and am having some difficulties.

I am using this example to base my code: http://msdn.microsoft.com/en-us/library/bb763516.aspx

I have my data object set up, but don't know what's going on when db.Orders.InsertOnSubmit is executed.

How can I create this db object to insert a data object into my database using InsertOnSubmit?

edit: Here is some code that I'm working with (It's probably a world of wrong, I'm pretty much fumbling in the dark at this point). I'm super new to database objects, so the whole concept is a bit confusing for me.

var Data = new Data();
Data.value1 = 1;
var db = new dbo(connectionString);
db.InsertOnSubmit(Data);
+2  A: 

You need to do more than just call InsertOnSubmit(). You also need to call SubmitChanges(). The reason these are separate steps is to allow you to have multiple inserts, updates and deletes and then submit them all at once via SubmitChanges().

Randy Minder
If cases where I have only one row to submit, is there a way to submit that row by calling only one function instead of calling both InsertOnSubmit() and SubmitChanges()?
Soo
No. You must call SubmitChanges(). Nothing gets persisted to the database until this method is called. Although, I suppose you could write your own Extension method that combines the two. But I don't really see the benefit in this.
Randy Minder