The next section explains how.
DataContext
Before you can execute a LINQ to SQL query, a connection to the data source must be made. In LINQ
to SQL, database connections are made through the DataContext class. Think of the DataContext class
on the same level as you would the SqlConnection class of ADO.NET. DataContext is the medium in
which connections to a database are made, through which objects are retrieved from and submitted to
the database.
Like the SqlConnection class, the DataContext instance accepts a connection string. Once the connection
is made, data is read from, and changes are transmitted back to, the database through the DataContext.
However, there is one thing the DataContext does that the SqlConnection does not. Since LINQ to SQL
deals with objects, the DataContext also does the work of converting the objects into SQL queries and
then reassembling the results back into queryable objects.
The DataContext has several overloads, one of which is just a simple connection string specifying the
connection information, as shown in this code:
DataContext db = new DataContext(
"Initial Catalog=AdventureWorks;Integrated i
Security=sspi");
You can also pass an IDbConnection, which represents an open connection to a data source.
Pages:
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359