1、使用对象关系设计器创建实体类
使用“LINQ to SQL类”模板在项目中添加一个新类。使用服务器资源管理器连接数据库,将表从数据库拖放到“对象关系设计器”中
2、在WPF控件中显示来自实体对象或集合的数据为控件的一个恰当的属性定义绑定。如果控件显示一个对象列表,就将控件的DataContext属性设为一个实体对象集合。如果控件显示单个对象的数据,就将控件的DataContext属性设为一个实体对象,并在绑定的Path属性中指定要显示实体对象的哪个属性的值
3、使用DLINQ修改数据库中的信息首先采取以下操作之一:
为了更新数据库的表中的一行,将该行的数据取回到一个实体对象中,然后将新值赋给实体对象的恰当的属性;
要在数据库的表中插入一个新行,请新建对应实体类的一个实例,设置它的属性,然后调用恰当的Table集合的Add方法,将新的实体对象作为参数传递;
要从数据库的表中删除一行,请调用恰当的Table集合的Remove方法,将要删除的实体对象作为参数传递。
之后,在完成了所有更改之后,调用DataContext对象的SubmitChanges方法,将这些更改送回数据库。
如:
1
private void productsList_KeyDown(object sender, KeyEventArgs e)
2
{
3
switch (e.Key)
4
{
5
case Key.Enter: editProduct(this.productsList.SelectedItem as Product);
6
break;
7
case Key.Insert: addNewProduct();
8
break;
9
case Key.Delete: deleteProduct(this.productsList.SelectedItem as Product);
10
break;
11
}
12
13
}
14
15
private void editProduct(Product product)
16
{
17
ProductForm pf = new ProductForm();
18
pf.Title = "Edit Product Details";
19
pf.productName.Text = product.ProductName;
20
pf.quantityPerUnit.Text = product.QuantityPerUnit;
21
pf.unitPrice.Text = product.UnitPrice.ToString();
22
23
if (pf.ShowDialog().Value)
24
{
25
product.ProductName = pf.productName.Text;
26
product.QuantityPerUnit = pf.quantityPerUnit.Text;
27
product.UnitPrice = Decimal.Parse(pf.unitPrice.Text);
28
this.saveChanges.IsEnabled = true;
29
30
}
31
32
}
33
34
private void addNewProduct()
35
{
36
ProductForm pf = new ProductForm();
37
pf.Title = "New Product for" + supplier.CompanyName;
38
if (pf.ShowDialog().Value)
39
{
40
Product newProd = new Product();
41
newProd.SupplierID = supplier.SupplierID;
42
newProd.ProductName = pf.productName.Text;
43
newProd.QuantityPerUnit = pf.quantityPerUnit.Text;
44
newProd.UnitPrice = Decimal.Parse(pf.unitPrice.Text);
45
supplier.Product.Add(newProd);
46
47
try
48
{
49
productsInfo.Add(newProd);
50
}
51
catch (ArgumentOutOfRangeException e)
52
{
53
}
54
this.saveChanges.IsEnabled = true;
55
}
56
}
57
58
private void deleteProduct(Product product)
59
{
60
MessageBoxResult response = MessageBox.Show("删除" + product.ProductName, "询问", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);
61
if (response == MessageBoxResult.Yes)
62
{
63
supplier.Product.Remove(product);
64
productsInfo.Remove(product);
65
this.saveChanges.IsEnabled = true;
66
}
67
}
68
69
private void saveChanges_Click(object sender, RoutedEventArgs e)
70
{
71
try
72
{
73
ndc.SubmitChanges();
74
saveChanges.IsEnabled = false;
75
}
76
catch (Exception ex)
77
{
78
MessageBox.Show(ex.Message, "Error saving changes");
79
}
80
}
81
} XAML
1
<Window x:Class="Suppliers.SupplierInfo"
2
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4
xmlns:app="clr-namespace:Suppliers"
5
Title="SupplierInfo" Height="300" Width="484" Loaded="Window_Loaded">
6
<Window.Resources>
7
<app:PriceConverter x:Key="priceConverter"/>
8
<DataTemplate x:Key="SuppliersTemplate">
9
<StackPanel Orientation="Horizontal">
10
<TextBlock Text="{Binding Path=SupplierID}"/>
11
<TextBlock Text=":"/>
12
<TextBlock Text="{Binding Path=CompanyName}"/>
13
<TextBlock Text=":"/>
14
<TextBlock Text="{Binding Path=ContactName}"/>
15
</StackPanel>
16
</DataTemplate>
17
</Window.Resources>
18
<Grid>
19
<Grid.RowDefinitions>
20
<RowDefinition Height="231*" />
21
<RowDefinition Height="31*" />
22
</Grid.RowDefinitions>
23
<Button HorizontalAlignment="Left" Margin="11,0,0,8" Name="saveChanges" Width="101" IsEnabled="False" Grid.Row="1" Click="saveChanges_Click">SaveChanges</Button>
24
<ComboBox Height="23" Margin="11,26,12,0" Name="suppliersList" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding}" ItemTemplate="{StaticResource SuppliersTemplate}" VerticalAlignment="Top" HorizontalContentAlignment="Stretch" SelectionChanged="suppliersList_SelectionChanged" />
25
<ListView Margin="11,54,12,10" Name="productsList" KeyDown="productsList_KeyDown"
26
IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding}"
27
HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
28
<ListView.View>
29
<GridView>
30
<GridView.Columns>
31
<GridViewColumn Width="75" Header="Product ID" DisplayMemberBinding="{Binding Path=ProductID}"/>
32
<GridViewColumn Width="225" Header="Name" DisplayMemberBinding="{Binding Path=ProductName}"/>
33
<GridViewColumn Width="135" Header="Quantity Per Unit" DisplayMemberBinding="{Binding Path=QuantiryPerUnit}"/>
34
<GridViewColumn Width="75" Header="Unit Price" DisplayMemberBinding="{Binding Path=UnitPrice,Converter={StaticResource priceConverter}}" />
35
</GridView.Columns>
36
</GridView>
37
</ListView.View>
38
</ListView>
39
</Grid>
40
</Window>
41
4、使用DLINQ检测更新数据库时的冲突
写一个处理程序来处理ChangeConfilictException异常。在异常处理程序中,检查DataContext对象的ChangeConflicts属性中的ObjectChangeConflict对象。对于每个冲突,都决定最适合的解决方案。然后调用Resolve方法,并传递恰当的RefreshMode枚举值作为参数