By using LINQ to SQL, you can use the LINQ technology to access SQL databases just as you would access an in-memory collection.
For example, the dataBase object in the following code is created to represent the Adventure Works database, the Customers table is targeted, the rows are filtered for Customers from Dindigul, and a string for CompanyName is selected for retrieval.
When the loop is executed, the collection of CompanyName values is retrieved.
// Adventure Works inherits from System.Data.Linq.DataContext.
AdventureWorks nw = new AdventureWorks(@"AdventureWorks.mdf");
// or, if you are not using SQL Server Express
// AdventureWorks nw = new AdventureWorks ("Database=AdventureWorks;Server=server_name;Integrated Security=SSPI");
var companyNameQuery =
from cust in nw.Customers
where cust.City == "Dindigul"
select cust.CompanyName;
foreach (var customer in companyNameQuery)
{
Console.WriteLine(customer);
}
No comments:
Post a Comment