The strongly typed DataContext is discussed in the next section.
219
Part III: LINQ to SQL
The following example creates a new database using the name ScottWrox, which is specified in the
connection string:
DataContext db = new DataContext("Initial Catalog=ScottWrox;Integrated i
Security=sspi");
db.CreateDatabase();
The same criteria for database naming apply to the DeleteDatabase method.
db.DeleteDatabase();
The following example illustrates the use of the DatabaseExists method of the DataContext class, which
can be used to determine if a database already exists:
try
{
DataContext db = new DataContext("Initial Catalog=WroxScott;Integrated
Security=sspi");
bool_dbExists = db.DatabaseExists();
if (_dbExists == true)
db.DeleteDatabase();
else
{
db.CreateDatabase();
textBox1.Text = "Database created.";
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
The SubmitChanges method is the component that sends your data changes back to the database. Here??™s
how to use it:
DataContext db = new DataContext("Initial Catalog=AdventureWorks;Integrated i
Security=sspi");
// Do some work on the data
db.
Pages:
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361