SubmitChanges();
You??™ll explore SubmitChanges in more detail later in the chapter.
Strongly Typed DataContext
Creating a strongly typed DataContext is quite simple. All you need to do is create a new class that
inherits from the DataContext class, as shown in the code example below:
public class AdventureWorks : DataContext
{
220
Chapter 11: LINQ to SQL Queries
public AdventureWorks(string connection) : base(connection) {}
// table definitions
}
Once the DataContext is created, you can use it to connect to the specified database, as shown in the
following example. In it, the preceding strongly typed DataContext is given the same name as the
database to use; in this case, AdventureWorks. Therefore, no database name needs to be specified in
the connection string.
AdventureWorks db = new AdventureWorks(Integrated Security=sspi");
bool _dbExists = db.DatabaseExists();
if (_dbExists == true)
textBox1.Text = "Yep, Exists!";
Creating strongly typed DataContext objects is preferred over non-strongly typed because utilizing
strongly typed table objects eliminates the need to use the GetTable method in your queries.
Pages:
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362