It strongly types
the DataContext and includes a reference to a strongly typed table.
The bulk of your form code should now look like this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
public class AdventureWorks : DataContext
{
public AdventureWorks(string connection) : base(connection) {}
public Table
Contact;
}
The strongly typed DataContext class references a strongly typed table, Contact, which has not been
created yet. The following code does that. Add it below the strongly typed DataContext class:
[Table(Name = "Person.Contact")]
public class Contact
{
[Column(DBType = "int not null")]
public int ContactID;
222
Chapter 11: LINQ to SQL Queries
[Column(DBType = "nvarchar(8) not null")]
public string Title;
[Column(DBType = "nvarchar(50) not null")]
public string FirstName;
[Column(DBType = "nvarchar(50) not null")]
public string MiddleName;
[Column(DBType = "nvarchar(50) not null")]
public string LastName;
[Column(DBType = "nvarchar(50) not null")]
public string EmailAddress;
[Column(DBType = "int")]
public int EmailPromotion;
[Column(DBType = "bit")]
public byte NameStyle;
[Column(DBType = "varchar(40)")]
public string PasswordHash;
[Column(DBType = "varchar(40)")]
public string PasswordSalt;
}
Next, add the following code to the Click event of the Insert button:
try
{
AdventureWorks db = new AdventureWorks("Integrated Security=sspi");
Contact con = new Contact();
con.
Pages:
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365