访问数据库通用类 VB.NET

访问数据库通用类  VB.NET
 
 这是在学习的 NET 的时候写的一个数据访问类 不完善的地方请网友指正。同样也适合初学者参考
Imports System.Data
Imports System.Data.SqlClient
Imports System.Net
Imports System.Net.Sockets
Imports System.IO
Imports System.Text.RegularExpressions
Namespace sunsoft
    Namespace Data
        '**********************************************************************************************************
        ' 类名:
        '      DataManeger
        '功能:
        '    完成数据基本操作 插入 删除 查询
        '构造函数:(重载了2次)
        'Public sub New() 无参构造
        '   构建数据库访问的基本对象 和 数据处理对象.没有指定字符连接串ConnectionString.如要用则需要设置ConnectionStrig属性
        '  
        'Public sub New(byval Connection as string) 有参构造
        '  构建数据库访问的基本对象 和 数据处理对象 使用时需要指定ConnectionString 进行构建
        '枚举定义:
        '  1):ExecSqlType  SQL执行类型
        '  2):ReturnType  枚举执行SQL语句后返回的类型
        '成员:
        '  1):Public property setConnection() as string 设置连接字符串
        '  2):Execsql       执行Sql 语句包括Insert Updata Delete Query ProcedureStore
        '  3):Dispose         释放各个对象的内存空间
        '**********************************************************************************************************
        Public Class DataManeger
            Private [my_conn] As SqlConnection = Nothing
            Private [my_cmd] As SqlCommand = Nothing
            Private gs_connection As String = ""

            Public Enum ExecSqlType ' 枚举SQL执行类型
                ExecSqlQuery    '执行数据查询
                ExecSqlInsert   '执行数据插入
                ExecSqlUpdate   '执行数据更新
                ExecSqlDelete   '执行数据删除
                ExecSqlStoreProc '执行存储过程
            End Enum

            Public Enum ReturnType '枚举返回类型
                returnNone '无返回值
                returnDataTable '返回数据表
                returnDataset '返回数据集
            End Enum

            Public Sub New() '无参构造
                [my_conn] = New SqlConnection
                [my_cmd] = New SqlCommand
                [my_cmd].Connection = [my_conn]
            End Sub

            Public Sub New(ByVal Connection As String) '有参构造
                [my_conn] = New SqlConnection
                [my_cmd] = New SqlCommand
                [my_conn].ConnectionString = Connection
                [my_cmd].Connection = [my_conn]
                gs_connection = Connection
            End Sub
            Public Property setConnection() As String '设置连接字符串
                Get
                    Return gs_connection
                End Get
                Set(ByVal value As String)
                    gs_connection = value
                End Set
            End Property
            '函数名:
            '      ExecSql
            '  参数:
            '           输入       Sql   字符型    Sql语句
            '                      ExecType Sql执行的类型 默认值是执行查询
            '                      ReturnType Sql执行后的返回类型 默认是返回 数据表
            '返回值:根据不同的对象返回的值

            Public Overridable Function ExecSql(ByVal Sql As String, Optional ByVal ExecSqlType As ExecSqlType _
                  = ExecSqlType.ExecSqlQuery, Optional ByVal ReturnType As ReturnType = ReturnType.returnDataTable) As Object
                Dim Ret As Object = Nothing

                Try

                    If [my_conn].State <> ConnectionState.Open Then [my_conn].Open()

                    Select Case ExecSqlType
                        Case DataManeger.ExecSqlType.ExecSqlInsert, DataManeger.ExecSqlType.ExecSqlDelete, _
                               DataManeger.ExecSqlType.ExecSqlUpdate
                            [my_cmd].CommandText = Sql
                            [my_cmd].ExecuteNonQuery()
                        Case DataManeger.ExecSqlType.ExecSqlQuery
                            Dim my_dataapder As New SqlDataAdapter(Sql, [my_conn])

                            If ReturnType = DataManeger.ReturnType.returnDataset Then '返回数据集
                                Dim my_dataset As New DataSet
                                my_dataapder.Fill(my_dataset)
                                Ret = my_dataset
                                my_dataset.Dispose()
                                my_dataset = Nothing
                            Else '返回数据表
                                Dim my_table As New DataTable
                                my_dataapder.Fill(my_table)
                                Ret = my_table
                                my_table.Dispose()
                                my_table = Nothing
                            End If

                            my_dataapder.Dispose()
                            my_dataapder = Nothing

                        Case DataManeger.ExecSqlType.ExecSqlStoreProc

                            If ReturnType = DataManeger.ReturnType.returnDataset OrElse _
                                                              ReturnType = DataManeger.ReturnType.returnDataset Then
                                Dim my_dataader As New SqlDataAdapter(Sql, [my_conn])

                                If ReturnType = DataManeger.ReturnType.returnDataset Then
                                    Dim my_dataset As New DataSet
                                    my_dataader.Fill(my_dataset)
                                    Ret = my_dataader
                                    my_dataset.Dispose()
                                    my_dataset = Nothing
                                Else '返回数据表
                                    Dim my_table As New DataTable
                                    my_dataader.Fill(my_table)
                                    Ret = my_table
                                    my_table.Dispose()
                                    my_table = Nothing
                                End If

                                my_dataader.Dispose()
                                my_dataader = Nothing
                            Else '无返回值
                                [my_cmd].CommandText = Sql
                                [my_cmd].ExecuteReader()
                                Ret = True
                            End If
                    End Select

                Catch ex As Exception
                    Ret = Nothing
                End Try

                Return Ret
            End Function

            Public Sub Dispose() '释放对象空间
                If [my_conn].State = ConnectionState.Open Then [my_conn].Close()
                [my_conn].Dispose()
                [my_conn] = Nothing
                [my_cmd].Dispose()
                [my_cmd] = Nothing
            End Sub
        End Class
    End Namespace

    Namespace Net
        Public Class Mynet
            Private Shared Function getPagecontent(ByVal Url As String) As String
                Dim Ret As String = ""
                Dim Webquet As HttpWebRequest = WebRequest.Create(Url) '创建一个请求

                Try
                    Dim WebRes As HttpWebResponse = Webquet.GetResponse()
                    Dim str As New StreamReader(WebRes.GetResponseStream)
                    Ret = str.ReadToEnd
                    WebRes.Close()
                    str.Close()
                    str.Dispose()

                Catch ex As Exception
                    ret = ex.Message
                End Try
                Webquet.Abort() '取消请求
                Return Ret
            End Function

            Public Function getoutIP(Optional ByVal Url As String = "http://www.163.com") As String
                Dim ip As String = ""
                Try
                    Dim patt As String = "IP:/[(?<IP>[0-9/.]*)/]"
                    ip = Regex.Match(getPagecontent(Url), patt).Groups("IP").Value
                Catch ex As Exception
                    ip = ""
                End Try
                Return ip
            End Function
        End Class
    End Namespace
End Namespaces

 
 

阅读更多

没有更多推荐了,返回首页