构建数据层对象--学习源码 Duwamish7

      .Net 学习 2005-3-15 21:37
在n层程序设计中的数据层,就是一些用来描述、存放、传递“一套”数据的类。

下面把示例代码BookData.vb放上来供我copy。

引用:

'----------------------------------------------------------------
' Copyright (C) 2000-2001 Microsoft Corporation
' All rights reserved.
'
' This source code is intended only as a supplement to Microsoft
' Development Tools and/or on-line documentation. See these other
' materials for detailed information regarding Microsoft code samples.
'
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY 
' OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT 
' LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR 
' FITNESS FOR A PARTICULAR PURPOSE.
'----------------------------------------------------------------
Option Strict On
Option Explicit On

Imports System
Imports System.Data
Imports System.Runtime.Serialization

Namespace Duwamish7.Common.Data

    '----------------------------------------------------------------
    ' Namespace: Duwamish7.Common.Data
    ' Class: BookData
    '
    ' Description: 
    '   A custom serializable dataset containing book information.
    '----------------------------------------------------------------
    <System.ComponentModel.DesignerCategory("Code"), SerializableAttribute> Public Class BookData
        Inherits DataSet
        '
        ' Books table constants
        '
        Public Const BOOKS_TABLE As String = "Books"
        Public Const PKID_FIELD As String = "PKId"
        Public Const TYPE_ID_FIELD As String = "TypeId"
        Public Const PUBLISHER_ID_FIELD As String = "PublisherId"
        Public Const PUBLICATION_YEAR_FIELD As String = "PublicationYear"
        Public Const ISBN_FIELD As String = "ISBN"
        Public Const IMAGE_FILE_SPEC_FIELD As String = "ImageFileSpec"
        Public Const TITLE_FIELD As String = "Title"
        Public Const DESCRIPTION_FIELD As String = "Description"
        Public Const UNIT_PRICE_FIELD As String = "UnitPrice"
        Public Const UNIT_COST_FIELD As String = "UnitCost"
        Public Const ITEM_TYPE_FIELD As String = "ItemType"
        Public Const PUBLISHER_NAME_FIELD As String = "PublisherName"
        Public Const AUTHORS_FIELD As String = "Authors"
        
        '----------------------------------------------------------------
        ' Search Type Enum
        ' Each item is currently ordered and valued to match the layout in 
        '   advsrch.cls changing these values will cause problems with 
        '   functionality in the advsrch class.
        '----------------------------------------------------------------

        <SerializableAttribute()> Public Enum SearchTypeEnum		
            Title = 0
            ISBN = 1
            Author = 2
            Subject = 3
            ID = 4
            IdList = 5
        End Enum
        
        '----------------------------------------------------------------
        ' Sub New:
        '   Constructor to support serialization.
        '----------------------------------------------------------------
		
        Private Sub New(info as SerializationInfo , context as StreamingContext) 
            MyBase.New(info, context) 
        End Sub

		
        '----------------------------------------------------------------
        ' Sub New:
        '   Initialize a BookData instance by building the table schema.
        '----------------------------------------------------------------
        Public Sub New()
            MyBase.New
            '
            ' Create the tables in the dataset
            '
            BuildDataTables
        End Sub
                
        '----------------------------------------------------------------
        ' Sub BuildDataTables:
        '   Creates the following datatables:  Books
        '----------------------------------------------------------------
        Private Sub BuildDataTables()
            '
            ' Create the Books table
            '
            Dim table As DataTable = New DataTable(BOOKS_TABLE)
            
            With table.Columns
                .Add(PKID_FIELD, GetType(System.Int32))
                .Add(TYPE_ID_FIELD, GetType(System.Int32))
                .Add(PUBLISHER_ID_FIELD, GetType(System.Int32))
                .Add(PUBLICATION_YEAR_FIELD, GetType(System.Int16))
                .Add(ISBN_FIELD, GetType(System.String))
                .Add(IMAGE_FILE_SPEC_FIELD, GetType(System.String))
                .Add(TITLE_FIELD, GetType(System.String))
                .Add(DESCRIPTION_FIELD, GetType(System.String))
                .Add(UNIT_PRICE_FIELD, GetType(System.Decimal))
                .Add(UNIT_COST_FIELD, GetType(System.Decimal))
                .Add(ITEM_TYPE_FIELD, GetType(System.String))
                .Add(PUBLISHER_NAME_FIELD, GetType(System.String))
                '
                ' [Authors] is an optional column that will get added dynamically to the table schema
                ' when the stored proc. GetBookById is used to fill the 'Books' table
                '
            End With
            
            Me.Tables.Add(table)
        End Sub      
        
    End Class 'BookData
    
End Namespace 'Duwamish7.Common.Data


标签集:TAGS:
回复Comments() 点击Count()

回复Comments

{commentauthor}
{commentauthor}
{commenttime}
{commentnum}
{commentcontent}
作者:
{commentrecontent}