Simple LINQ - SQL Connecting Program in C#

LINQ - Language Integrated Query.It provides the Querying ability to the database and also doing the DML operations such as insert, update and delete.for more about LINQ, have a look at http://msdn.microsoft.com/en-us/library/bb308959.aspx

There are infinite pages are available to describe the Linq, its functionality, and design. But for a beginner, let us go with creating a simple application using Linq and playing with database. Before writing the program, we need to know the following basic things.
1. DataContext

This is a Layer or cache kind of stuff which interacts between the database and application. The application uses the datacontext to manipulate data with the database.
2. dbml - Database Markup Language
Linq to SQL class is known as dbml. The dbml maintains the tables and linkings.

Now we start with creating simple program. The aim of the program is to display the Northwind customers data whose names are starting with "a" in a datagrid.
Step 1: Create new C# windows project in VS2008.
Step 2: Import SqlClient/Oledb classes for database usage.
Step 3: Create dbml file. Right click Project and add New Item. Choose the "LINQ to SQL Classes" from the templates section. Name it as you like, here let us name it as "SampleNwnd". The extension would be ".dbml".
The dbml file will be opened with empty designer window saying that "Create data classes by dragging items from "Server Explorer" or "Toolbox" onto this design surface.
Step 4: Click the "Server Explorer" link and create a data connection. Right Click "Data connection" and click "Add Connection". This will open up the "Add Connection" window which contains the SQL Server credentials, database and authentication. Choose the appropriate settings and click "Ok" to complete the connection.

Once the connection has been added, the database tables, schema, procedures, views everthing will be shown.
Just drag and drop the Tables, Views to the dbml design window. To create the linking/association between the selected tables, Right click dbml screen ==> Add ==> Association. The Association editor will be shown. From there we can bind the master and child table relationship.

The next thing is coding part. We need to use the dbml in the coding. We may play with database activities such as view, insert, update and delete. Here let us display the list of customers in Northwind database whose names are starting in "a".
Here is the simple code;

private void button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection conn = new SqlConnection("connection string");
conn.Open();
SampleNwndDataContext nwnd = new SampleNwndDataContext(conn);
var customers = from cust in nwnd.Customers
where cust.First_Name.StartsWith("a")
select cust;
dataGridView1.DataSource = customers;
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
The above code will display the data in a gridview. Here, "SampleNwndDataContext" is the data context class which we have added in the project. The tables which are added in the dbml file, those will be accessible inside the program. There are enormous options in LINQ to explore. But I believe this will be a starting point on that.
For more sample programs on LINQ, have a look at http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx

Comments

Popular posts from this blog

Storing Images in MySQL database

XML Documentation comments

DataTable ExtendedProperties