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枚舉值作為參數