数据访问层实现与WPF应用开发
立即解锁
发布时间: 2025-08-20 01:13:16 订阅数: 1 


C#面向对象编程与Web服务开发
### 数据访问层实现与WPF应用开发
#### 1. 数据访问层操作
在开发应用程序时,数据访问层的实现至关重要。以下是一些关键操作步骤:
- **加载数据到网格**:
1. 在窗体设计器中打开Form2,双击“Get Data”按钮,在代码编辑器中打开按钮点击事件方法。
2. 在btnGetData点击事件过程中添加以下代码,以执行StoreSales类中定义的GetData方法,并将Stores表加载到Stores网格,同时设置Sales网格的DataMember属性以加载所选商店的销售数据。
```csharp
private void btnGetData_Click(object sender, EventArgs e)
{
StoreSales storeSales = new StoreSales();
StoreSalesDataSet = storeSales.GetData();
dgvStores.DataSource = StoreSalesDataSet.Tables["Stores"];
dgvSales.DataSource = StoreSalesDataSet.Tables["Stores"];
dgvSales.DataMember = "StoresToSales";
}
```
3. 在代码编辑器中打开Program类,更改代码以在窗体加载时启动Form2。
```csharp
Application.Run(new Form2());
```
4. 窗体加载后,点击“Get Data”按钮加载网格。在Stores网格中选择新行应更新Sales网格以显示该商店的销售情况。测试完成后,停止调试器并退出Visual Studio。
#### 2. 实体框架的使用
实体框架(EF)是内置在ADO.NET中的对象关系映射(ORM)技术,它消除了.NET语言的面向对象编程结构与数据库系统的关系数据结构之间的不匹配。
- **使用实体框架的步骤**:
1. 要在应用程序中使用实体框架,首先需要向应用程序添加一个ADO.NET实体数据模型。这将启动实体数据模型向导,允许你从头开始开发模型或从现有数据库生成模型。
2. 选择从现有数据库生成模型,可创建与数据库的连接,并选择要包含在模型中的表、视图和存储过程。向导的输出是一个.edmx文件,该文件是一个基于XML的文件,包含三个部分:
- **存储架构定义语言(SSDL)**:描述数据存储的表和关系。以下是从Pubs数据库生成的数据模型的SSDL部分示例:
```xml
<EntityContainer Name="pubsModelStoreContainer">
<EntitySet Name="sales" EntityType="pubsModel.Store.sales"
store:Type="Tables" Schema="dbo" />
<EntitySet Name="stores" EntityType="pubsModel.Store.stores"
store:Type="Tables" Schema="dbo" />
<AssociationSet Name="FK__sales__stor_id__1273C1CD"
Association="pubsModel.Store.FK__sales__stor_id__1273C1CD">
<End Role="stores" EntitySet="stores" />
<End Role="sales" EntitySet="sales" />
</AssociationSet>
</EntityContainer>
<EntityType Name="sales">
<Key>
<PropertyRef Name="stor_id" />
<PropertyRef Name="ord_num" />
<PropertyRef Name="title_id" />
</Key>
<Property Name="stor_id" Type="char" Nullable="false" MaxLength="4" />
<Property Name="ord_num" Type="varchar" Nullable="false" MaxLength="20" />
<Property Name="ord_date" Type="datetime" Nullable="false" />
<Property Name="qty" Type="smallint" Nullable="false" />
<Property Name="payterms" Type="varchar" Nullable="false" MaxLength="12" />
<Property Name="title_id" Type="varchar" Nullable="false" MaxLength="6" />
</EntityType>
```
- **概念架构定义语言(CSDL)**:指定实体及其之间的关系,用于在应用程序中处理数据。以下是从Pubs数据库生成的数据模型的CSDL部分示例:
```xml
<EntityContainer Name="pubsEntities" annotation:LazyLoadingEnabled="true">
<EntitySet Name="sales" EntityType="pubsModel.sale" />
<EntitySet Name="stores" EntityType="pubsModel.store" />
<AssociationSet Name="FK__sales__stor_id__1273C1CD"
Association="pubsModel.FK__sales__stor_id__1273C1CD">
<End Role="stores" EntitySet="stores" />
<End Role="sales" EntitySet="sales" />
</AssociationSet>
</EntityContainer>
<EntityType Name="sale">
<Key>
<PropertyRef Name="stor_id" />
<PropertyRef Name="ord_num" />
<PropertyRef Name="title_id" />
</Key>
<Property Name="stor_id" Type="String" Nullable="false"
MaxLength="4" Unicode="false" FixedLength="true" />
<Property Name="ord_num" Type="String" Nullable="false"
MaxLength="20" Unicode="false" FixedLength="false" />
<Property Name="ord_date" Type="DateTime" Nullable="false" />
<Property Name="qty" Type="Int16" Nullable="false" />
<Property Name="payterms" Type="String" Nullable="false"
MaxLength="12" Unicode="false" FixedLength="false" />
<Property Name="title_id" Type="String" Nullable="false"
MaxLength="6" Unicode="false" FixedLength="false" />
<NavigationProperty Name="store"
Relationship="pubsModel.FK__sales__stor_id__1273C1CD"
FromRole="sales" ToRole="stores" />
</EntityType>
```
- **映射规范语言(MSL)**:将概念模型映射到存储模型。以下是从Pubs数据库生成的数据模型的MSL部分示例:
```xml
<EntityContainerMapping StorageEntityContainer="pubsModelStoreContainer"
CdmEntityContainer="pubsEntities">
<EntitySetMapping Name="sales"><EntityTypeMapping TypeName="pubsModel.sale">
<MappingFragment StoreEntitySet="sale
```
0
0
复制全文
相关推荐










