1、使用對(duì)象關(guān)系設(shè)計(jì)器創(chuàng)建實(shí)體類
使用“LINQ to SQL類”模板在項(xiàng)目中添加一個(gè)新類。使用服務(wù)器資源管理器連接數(shù)據(jù)庫(kù),將表從數(shù)據(jù)庫(kù)拖放到“對(duì)象關(guān)系設(shè)計(jì)器”中
2、在WPF控件中顯示來自實(shí)體對(duì)象或集合的數(shù)據(jù)為控件的一個(gè)恰當(dāng)?shù)膶傩远x綁定。如果控件顯示一個(gè)對(duì)象列表,就將控件的DataContext屬性設(shè)為一個(gè)實(shí)體對(duì)象集合。如果控件顯示單個(gè)對(duì)象的數(shù)據(jù),就將控件的DataContext屬性設(shè)為一個(gè)實(shí)體對(duì)象,并在綁定的Path屬性中指定要顯示實(shí)體對(duì)象的哪個(gè)屬性的值
3、使用DLINQ修改數(shù)據(jù)庫(kù)中的信息首先采取以下操作之一:
為了更新數(shù)據(jù)庫(kù)的表中的一行,將該行的數(shù)據(jù)取回到一個(gè)實(shí)體對(duì)象中,然后將新值賦給實(shí)體對(duì)象的恰當(dāng)?shù)膶傩裕?br />要在數(shù)據(jù)庫(kù)的表中插入一個(gè)新行,請(qǐng)新建對(duì)應(yīng)實(shí)體類的一個(gè)實(shí)例,設(shè)置它的屬性,然后調(diào)用恰當(dāng)?shù)腡able集合的Add方法,將新的實(shí)體對(duì)象作為參數(shù)傳遞;
要從數(shù)據(jù)庫(kù)的表中刪除一行,請(qǐng)調(diào)用恰當(dāng)?shù)腡able集合的Remove方法,將要?jiǎng)h除的實(shí)體對(duì)象作為參數(shù)傳遞。
之后,在完成了所有更改之后,調(diào)用DataContext對(duì)象的SubmitChanges方法,將這些更改送回?cái)?shù)據(jù)庫(kù)。
如:
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檢測(cè)更新數(shù)據(jù)庫(kù)時(shí)的沖突
寫一個(gè)處理程序來處理ChangeConfilictException異常。在異常處理程序中,檢查DataContext對(duì)象的ChangeConflicts屬性中的ObjectChangeConflict對(duì)象。對(duì)于每個(gè)沖突,都決定最適合的解決方案。然后調(diào)用Resolve方法,并傳遞恰當(dāng)?shù)腞efreshMode枚舉值作為參數(shù)