To insert a row into the database

  1. Create a new object that includes the column data to be submitted.
  2. Add the new object to the LINQ to SQL Table collection associated with the target table in the database.
  3. Submit the change to the database.

The following code example creates a new object of type Order and populates it with appropriate values. It then adds the new object to the Order collection. Finally, it submits the change to the database as a new row in the Orders table.

‘ Create a new Order object.
Dim ord As New Order With _
{.OrderID = 12000, _
.ShipCity = “Seattle”, _
.OrderDate = DateTime.Now}

Add the new object to the Orders collection.
db.orders.insertonsubmit(ord)

‘ Submit the change to the database.
Try
db.SubmitChanges()
Catch e As Exception
Console.WriteLine(e)
‘ Make some adjustments.
‘ …
‘ Try again.
db.SubmitChanges()
End Try

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: