Serval times this question has been asked from me that how to create linq to SQL connection in asp.net. So, i have decided to write this example here.
In this example, I have used northwind database. First, I have created a connection string and establish a connection between object to database, then select [categoryname] from [categories] table.Finally, through loop i have display the category names on the page.
Here’s the solution
string connectionString = "Data Source=.;Initial Catalog=Northwind;Integrated Security=True";
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (NorthwindDataContext context = new NorthwindDataContext(connectionString))
{
var query = from c in context.Categories
select c.CategoryName;
foreach (var title in query)
{
Response.Write(title);
}
}
}