INSERTING DATA TO DATABASE USING LINQ TO SQL
Few months ago, I’ve created a simple demo about “Creating a Simple Registration Form using the ADO.NET way”. In this article, I'm going to demonstrate how to create a simple form that would allows users to insert data to the database using L2S.
As an overview, LINQ to SQL is a technology that allow you to query sql server. LINQ to SQL is an O/RM (object relational mapping) implementation that ships in the .NET Framework "Orcas" release, and which allows you to model a relational database using .NET classes. You can then query the database using LINQ, as well as update/insert/delete data from it.
I will not cover much on details about it in this article so if you need to know more about this technology then you can refer to this link: http://msdn.microsoft.com/en-us/library/bb425822.aspx
STEP 1: Creating a new Website in Visual Studio
To get started then lets go ahead and fire up Visual Studio 2008 and create a new WebSite by selecting File > New WebSite.
STEP 2: Adding a DBML file
Since we are going to use L2S then we need to add .dbml file. To do this, just right click on the application root and select Add New Item. On the template select LINQ to SQL Classes file. See below screen shot:
From there we can fill in those fields with values we want. Just for this demo, notice that I have filled in those fields with a sample data. Hitting the save button will invoke the method SaveCustomerInfo() which is responsible for doing the insert operation. Now if we look at the database we can see that the data we entered was successfully being saved to the database. See the screen shot below: