⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wblprojects.vb

📁 数据库学习的绝好例子简单的数据库经典入门
💻 VB
字号:
Public Class WBLProjects
    Implements IDisposable

    'Private variables and objects
    Private objWDAProject As WroxDataAccess.WDAProjects

    Private disposed As Boolean = False

#Region " Constructor and Destructor "
    Public Sub New(ByVal Company As String, ByVal Application As String)
        objWDAProject = New WroxDataAccess.WDAProjects(Company, Application)
    End Sub

    ' IDisposable
    Private Overloads Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposed Then
            If disposing Then
                ' TODO: put code to dispose managed resources
            End If

            objWDAProject.Dispose()
            objWDAProject = Nothing
        End If
        Me.disposed = True
    End Sub
#End Region

#Region " IDisposable Support "
    ' This code added by Visual Basic to correctly implement the disposable pattern.
    Public Overloads Sub Dispose() Implements IDisposable.Dispose
        ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub

    Protected Overrides Sub Finalize()
        ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        Dispose(False)
        MyBase.Finalize()
    End Sub
#End Region

#Region " Public Project Functions "
    Public Function GetProjects() As DataSet
        Try
            'Call the data component to get all projects
            GetProjects = objWDAProject.GetProjects

            'Loop through the DataSet and convert all DBNull values
            'to empty strings
            For intindex As Integer = 0 To _
                GetProjects.Tables("Projects").Rows.Count - 1

                'If the column contains a null value...
                If IsDBNull(GetProjects.Tables("Projects").Rows( _
                    intindex).Item("ProjectDescription")) Then

                    'Convert it to an empty string
                    GetProjects.Tables("Projects").Rows( _
                        intindex).Item("ProjectDescription") = String.Empty
                End If
            Next
        Catch ExceptionErr As Exception
            Throw New System.Exception(ExceptionErr.Message, _
                ExceptionErr.InnerException)
        End Try
    End Function

    Public Function GetProject(ByVal ProjectID As Guid) As DataSet
        Try
            'Call the data component to get a specific project
            GetProject = objWDAProject.GetProject(ProjectID)

            'If the column contains a null value...
            If IsDBNull(GetProject.Tables("Project").Rows(0).Item( _
                "ProjectDescription")) Then
                'Convert it to an empty string
                GetProject.Tables("Project").Rows(0).Item( _
                    "ProjectDescription") = String.Empty
            End If
        Catch ExceptionErr As Exception
            Throw New System.Exception(ExceptionErr.Message, _
                ExceptionErr.InnerException)
        End Try
    End Function

    Public Function GetNewProjectDS() As DataSet
        Try
            'Instantiate a new DataSet object
            GetNewProjectDS = New DataSet

            'Create a DataTable object
            Dim objDataTable As DataTable = GetNewProjectDS.Tables.Add("Project")

            'Create a DataColumn object
            Dim objDataColumn As DataColumn

            'Instantiate a new DataColumn and set its properties
            objDataColumn = New DataColumn("ProjectID", _
                Type.GetType("System.Guid"))
            objDataColumn.AllowDBNull = False

            'Add the column to the table
            objDataTable.Columns.Add(objDataColumn)

            'Instantiate a new DataColumn and set its properties
            objDataColumn = New DataColumn("ProjectName", _
                Type.GetType("System.String"))
            objDataColumn.AllowDBNull = False
            objDataColumn.MaxLength = 50

            'Add the column to the table
            objDataTable.Columns.Add(objDataColumn)

            'Instantiate a new DataColumn and set its properties
            objDataColumn = New DataColumn("ProjectDescription", _
                Type.GetType("System.String"))

            'Add the column to the table
            objDataTable.Columns.Add(objDataColumn)

            'Instantiate a new DataColumn and set its properties
            objDataColumn = New DataColumn("SequenceNumber", _
                Type.GetType("System.Byte"))
            objDataColumn.AllowDBNull = False

            'Add the column to the table
            objDataTable.Columns.Add(objDataColumn)
        Catch ExceptionErr As Exception
            Throw New System.Exception(ExceptionErr.Message, _
                ExceptionErr.InnerException)
        End Try
    End Function

    Public Function AddProject(ByVal Project As DataSet) As Boolean
        Try
            'Validate project data
            ValidateProjectData(Project)

            'Call the data component to add the project
            Return objWDAProject.AddProject(Project)
        Catch ExceptionErr As Exception
            Throw New System.Exception(ExceptionErr.Message, _
                ExceptionErr.InnerException)
        End Try
    End Function

    Public Function UpdateProject(ByVal Project As DataSet) As Boolean
        Try
            'Validate project data
            ValidateProjectData(Project)

            'Call the data component to update the project
            Return objWDAProject.UpdateProject(Project)
        Catch ExceptionErr As Exception
            Throw New System.Exception(ExceptionErr.Message, _
                ExceptionErr.InnerException)
        End Try
    End Function

    Public Function DeleteProject(ByVal ProjectID As Guid) As Boolean
        Try
            'Call the data component to delete the project
            Return objWDAProject.DeleteProject(ProjectID)
        Catch ExceptionErr As Exception
            Throw New System.Exception(ExceptionErr.Message, _
                ExceptionErr.InnerException)
        End Try
    End Function
#End Region

#Region " Private Validation Functions "
    Private Sub ValidateProjectData(ByRef Project As DataSet)
        'Validate the Name exists
        If Project.Tables("Project").Rows(0).Item( _
            "ProjectName").ToString.Trim.Length = 0 Then
            Throw New System.Exception( _
                "Project Name is a required field.")
        End If

        'Validate Description value
        If Not IsDBNull(Project.Tables("Project").Rows(0).Item( _
            "ProjectDescription")) Then
            If Project.Tables("Project").Rows(0).Item( _
                "ProjectDescription") = String.Empty Or _
                Project.Tables("Project").Rows(0).Item( _
                "ProjectDescription").ToString.Trim.Length = 0 Then
                'Set it to a null value
                Project.Tables("Project").Rows(0).Item( _
                    "ProjectDescription") = DBNull.Value
            Else
                'Trim spaces
                Project.Tables("Project").Rows(0).Item("ProjectDescription") = _
                    Project.Tables("Project").Rows(0).Item( _
                    "ProjectDescription").ToString.Trim
            End If
        End If

        'Validate the Sequence Number
        If Project.Tables("Project").Rows(0).Item( _
            "SequenceNumber") = 0 Then
            Throw New System.Exception( _
                "Sequence Number must contain a value between 1 and 255.")
        End If

        'Trim spaces
        Project.Tables("Project").Rows(0).Item("ProjectName") = _
            Project.Tables("Project").Rows(0).Item("ProjectName").ToString.Trim
    End Sub

#End Region
End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -