Database Reference
In-Depth Information
string connString = @"server = .\sql2012;integrated security = true;
database = AdventureWorks";
try
{
// Create data context
DataContext db = new DataContext(connString);
// Create typed table
Table<Contact> contacts = db.GetTable<Contact>();
// Query database
var contactDetails =
from c in contacts
where c.Title == "Mr."
orderby c.FirstName
select c;
// Display contact details
foreach (var c in contactDetails)
{
txtLinqtoSql.AppendText(c.Title);
txtLinqtoSql.AppendText("\t");
txtLinqtoSql.AppendText(c.FirstName);
txtLinqtoSql.AppendText("\t");
txtLinqtoSql.AppendText(c.LastName);
txtLinqtoSql.AppendText("\n");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
7. Now, to set the LinqToSql form as the start-up form, open Program.cs in the
code editor, and modify Application.Run(new LinqToObjects()); to be
Application.Run(new LinqToSql()); .
8. Build the solution, and then run the program by pressing Ctrl+F5; you should
see the results shown in Figure 18-7.
Search WWH ::




Custom Search