接口定义:

1 Public Interface IDataRowPersistable
2     ReadOnly Property PrimaryKey() As Object
3     Sub Save(ByVal row As DataRow)
4     Sub Load(ByVal row As DataRow)
5 End Interface

实现接口的类:

代码

 1 Public Class Student
 2     Implements IDataRowPersistable
 3 
 4     Property FirstName As String
 5     Property LastName As String
 6     Property BirthDate As Date
 7 
 8     Private ID As Guid = Guid.Empty
 9 
10     Public Sub New()
11     End Sub
12 
13     Public Sub New(ByVal firstName As String, ByVal lastName As String, ByVal birthDate As Date)
14         Me.FirstName = firstName
15         Me.LastName = lastName
16         Me.BirthDate = birthDate
17     End Sub
18 
19     Public ReadOnly Property PrimaryKey() As Object Implements IDataRowPersistable.PrimaryKey
20         Get
21             If Me.ID.Equals(Guid.Empty) Then Me.ID = Guid.NewGuid()
22             Return Me.ID
23         End Get
24     End Property
25 
26     Public Sub Load(ByVal row As DataRow) Implements IDataRowPersistable.Load
27         Me.ID = CType(row("ID"), Guid)
28         Me.FirstName = CStr(row("FirstName"))
29         Me.LastName = CStr(row("LastName"))
30         Me.BirthDate = CDate(row("BirthDate"))
31     End Sub
32 
33     Public Sub Save(ByVal row As DataRow) Implements IDataRowPersistable.Save
34         If DBNull.Value.Equals(row("ID")) Then row("ID") = Me.ID
35         row("FirstName") = Me.FirstName
36         row("LastName") = Me.LastName
37         row("BirthDate") = Me.BirthDate
38     End Sub
39 End Class
40 

用法:

代码

 1 Module Module1
 2     Sub InterfacesAndPolymorphism()
 3         Dim table As New DataTable()
 4 
 5         Dim idCol As DataColumn = table.Columns.Add("ID", GetType(Guid))
 6         table.PrimaryKey = New DataColumn() {idCol}
 7 
 8         table.Columns.Add("FirstName", GetType(String))
 9         table.Columns.Add("LastName", GetType(String))
10         table.Columns.Add("BirthDate", GetType(Date))
11 
12         Dim students() As Student = {
13             New Student("John", "Doe", #1/2/1965#),
14             New Student("Ann", "Doe", #8/17/1972#),
15             New Student("Robert", "Smith", #11/1/1973#)}
16 
17         SaveObjects(table, students)
18 
19         Dim studArray(table.Rows.Count - 1) As Student
20         For i As Integer = 0 To studArray.Count - 1
21             studArray(i) = New Student()
22         Next
23 
24         LoadObjects(table, studArray)
25     End Sub
26 
27     Sub SaveObjects(ByVal table As DataTable, ByVal array() As IDataRowPersistable)
28         ' Retrieve the primary key name. (Multiple column keys aren't supported.)
29         Dim pkName As String = table.PrimaryKey(0).ColumnName
30 
31         Dim dataView As New DataView(table)
32         dataView.Sort = pkName
33 
34         For Each obj As IDataRowPersistable In array
35             Dim row As DataRow
36             Dim rowIndex As Integer = dataView.Find(obj.PrimaryKey)
37 
38             If rowIndex >= 0 Then
39                 row = table.Rows(rowIndex)
40             Else
41                 row = table.NewRow()
42             End If
43             obj.Save(row)
44             If rowIndex < 0 Then table.Rows.Add(row)
45         Next
46     End Sub
47 
48     Sub LoadObjects(ByVal table As DataTable, ByVal array() As IDataRowPersistable)
49         For i As Integer = 0 To table.Rows.Count - 1
50             Dim row As DataRow = table.Rows(i)
51             array(i).Load(row)
52         Next
53     End Sub
54 End Module
55 

转载于:https://www.cnblogs.com/cuishengli/archive/2010/04/25/1719797.html

IDataRowPersistable相关推荐

最新文章

  1. CString 操作函数
  2. js脚本捕获页面 GET 方式请求的参数?其实直接使用 window.location.search 获得
  3. php $that,PHP中$this和$that指针使用实例
  4. 初始jquery事件-动态添加的新元素没有绑定上旧元素的事件
  5. 工控安全PLC固件逆向二
  6. 人力资源管理系统需求分析说明书
  7. Rasa课程、Rasa培训、Rasa面试系列之: Rasa客户案例T-Mobile电信公司
  8. 用户个人中心页面html5源码,橙色的商城个人中心全部页面模板html源码
  9. python apkg_GitHub - TonyDongGuaPi/pc_wxapkg_decrypt_python: PC微信小程序 wxapkg 解密
  10. 司空见惯 - 大哲学家康德的作息时间表
  11. 纯音乐自制吉他及钢琴简谱合集
  12. 代码统计工具cloc使用
  13. U盘文件乱码?修复后U盘文件消失,但仍占有U盘空间?
  14. 深度学习框架之Keras入门教程
  15. java作为微信小程序的后端_微信小程序连接java后端
  16. IP地址、URL及域名的相关概念
  17. 2017年,电视到底该怎么买?
  18. 普通话测试范读作品14号-和时间赛跑
  19. scrapy爬取猫眼电影及详情页
  20. java反编译微信小程序_微信小程序反编译的实现

热门文章

  1. 定义并调用函数输出 fibonacci 序列_科学网—Zmn-0351 薛问天:再谈数学概念的定义,评新华先生《0345》...
  2. 力扣长度最小的子数组
  3. because the following virtual functions are pure within
  4. LeetCode 572. 另一个树的子树 思考分析
  5. WEG的完整形式是什么?
  6. c# 声明类的时候初始化类_使用C#初始化的列表声明
  7. php的create_function、function_exists判断函数是否存在
  8. 2013_nanjing_online
  9. 笨小熊 -- ACM解决方法
  10. 排序(Sort)--【一】