Writing the Connection String

Different from classic LINQ to SQL, when you work with SQL Compact databases, you need to manually pass the connection string to the database; this is because the class generation could not take advantage of the IDE automation. This means that when you declare an instance of the DataContext class, you need to pass the connection string. For example, instantiating the DataContext for Northwind would be something like this:

image

Summary

LINQ to SQL is a built-in object relational mapping engine for Microsoft SQL Server databases. The engine maps database information such as tables and columns into .NET objects such as classes and properties, allowing working in a disconnected fashion against an object model rather than against the database. Mapped classes are known as entities. Adding LINQ to SQL classes to your projects can provide the ability of using LINQ for both querying entities and performing CRUD operations via specific methods offered by the DataContext class, which is responsible for managing the connection and entities during an application’s lifetime, including keeping track of changes that can be submitted to the database in one shot. LINQ to SQL also offers a trace log to understand what SQL instructions were sent to the database and provides the ability of handling optimistic concurrency as much as validating data taking advantage of partial methods. Finally, you can still write your queries the old-fashioned way sending SQL instructions directly to the data source. LINQ to SQL is useful if you need to work with a light weight or/m and if you are limited to SQL Server databases. If you instead need something more flexible and powerful, you should consider the ADO.NET Entity Framework discussed in next chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.189.5