DataObject.vb
上传用户:wj57717022
上传日期:2014-12-16
资源大小:4093k
文件大小:32k
源码类别:

医药行业

开发平台:

Visual Basic

  1. Imports System.Data
  2. Imports System.Data.SqlClient
  3. Imports System.Data.SqlTypes
  4. Imports System.Xml
  5. Imports System.Collections
  6. Imports System.Diagnostics
  7. Namespace DataAccess
  8.     Public Class DataObject
  9.         Inherits System.ComponentModel.Component
  10. #Region " 组件设计器生成的代码 "
  11.         Public Sub New(ByVal Container As System.ComponentModel.IContainer)
  12.             MyClass.New()
  13.             'Windows.Forms 类撰写设计器支持所必需的
  14.             Container.Add(Me)
  15.         End Sub
  16.         Public Sub New()
  17.             MyBase.New()
  18.             '该调用是组件设计器所必需的。
  19.             InitializeComponent()
  20.             privateModuleName = Me.GetType.ToString
  21.             '在 InitializeComponent() 调用之后添加任何初始化
  22.         End Sub
  23.         '组件重写 dispose 以清理组件列表。
  24.         Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  25.             If disposing Then
  26.                 If Not (components Is Nothing) Then
  27.                     components.Dispose()
  28.                 End If
  29.             End If
  30.             MyBase.Dispose(disposing)
  31.         End Sub
  32.         '组件设计器所必需的
  33.         Private components As System.ComponentModel.IContainer
  34.         '注意: 以下过程是组件设计器所必需的
  35.         '可以使用组件设计器修改此过程。
  36.         '不要使用代码编辑器修改它。
  37.         <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  38.             components = New System.ComponentModel.Container
  39.         End Sub
  40. #End Region
  41. #Region "定义变量和对象"
  42.         '定义ADO.Net对象
  43.         Private privateConnection As SqlConnection
  44.         Private privateCommand As SqlCommand
  45.         Private privateDataReader As SqlDataReader
  46.         Private privateXMLReader As XmlReader
  47.         Private privateSQLDataAdapter As SqlDataAdapter
  48.         Private privateDataSet As DataSet
  49.         '定义调用存储过程的参数数组
  50.         Private privateParameterList As ArrayList = New ArrayList
  51.         '定义连接对象的变量
  52.         Private privateConnectionString As String
  53.         Private privateServer As String
  54.         Private privateDatabase As String
  55.         Private privateUserName As String
  56.         Private privatePassword As String
  57.         '定义名称,释放内存对象,错误处理对象变量
  58.         Private privateModuleName As String
  59.         Private privateDisposedBoolean As Boolean
  60.         Private Const privateExceptionMessage As String = "数据应用错误。错误信息可能记录在事件日志中"
  61.         '定义SQL数据类型
  62.         Public Enum SQLDataType
  63.             SQLString
  64.             SQLChar
  65.             SQLInteger
  66.             SQLBit
  67.             SQLDateTime
  68.             SQLDecimal
  69.             SQLMoney
  70.             SQLImage
  71.         End Enum
  72. #End Region
  73. #Region "DataObject 构造函数"
  74.         Public Sub New(ByVal ConnectionString As String)
  75.             MyBase.New()
  76.             privateConnectionString = ConnectionString
  77.             privateModuleName = Me.GetType.ToString
  78.         End Sub
  79.         '定义带参数的构造函数
  80.         Public Sub New(ByVal Server As String, ByVal Database As String, ByVal UserName As String, ByVal Password As String)
  81.             MyBase.New()
  82.             privateConnectionString = "Server=" + Server + ";Database=" + Database + ";User ID=" + UserName + ";Password=" + Password + ";"
  83.             privateModuleName = Me.GetType.ToString
  84.         End Sub
  85. #End Region
  86. #Region "Public Properties"
  87.         'ConnectionString
  88.         Public Property ConnectionString() As String
  89.             Get
  90.                 Try
  91.                     Return privateConnection.ConnectionString
  92.                 Catch
  93.                     Return ""
  94.                 End Try
  95.             End Get
  96.             Set(ByVal Value As String)
  97.                 privateConnectionString = Value
  98.             End Set
  99.         End Property
  100.         Public Property Server() As String
  101.             Get
  102.                 Try
  103.                     Return privateUserName
  104.                 Catch
  105.                     Return ""
  106.                 End Try
  107.             End Get
  108.             Set(ByVal Value As String)
  109.                 privateUserName = Value
  110.             End Set
  111.         End Property
  112.         Public Property Database() As String
  113.             Get
  114.                 Try
  115.                     Return privateDatabase
  116.                 Catch
  117.                     Return ""
  118.                 End Try
  119.             End Get
  120.             Set(ByVal Value As String)
  121.                 privateDatabase = Value
  122.             End Set
  123.         End Property
  124.         Public Property UserName() As String
  125.             Get
  126.                 Try
  127.                     Return privateUserName
  128.                 Catch
  129.                     Return ""
  130.                 End Try
  131.             End Get
  132.             Set(ByVal Value As String)
  133.                 privateUserName = Value
  134.             End Set
  135.         End Property
  136.         Public Property Password() As String
  137.             Get
  138.                 Try
  139.                     Return privatePassword
  140.                 Catch
  141.                     Return ""
  142.                 End Try
  143.             End Get
  144.             Set(ByVal Value As String)
  145.                 privatePassword = Value
  146.             End Set
  147.         End Property
  148. #End Region
  149. #Region "DataObject 函数"
  150. #Region "RunSQLDataSet - 输入SQL语句返回DataSet"
  151.         '返回DataSet
  152.         'sql:查询字符串
  153.         Public Function runSQLDataSet(ByVal SQL As String, Optional ByVal TableName As String = Nothing) As DataSet
  154.             'SQL长度不得小于10个字节 
  155.             ValidateSQLStatement(SQL)
  156.             Try
  157.                 '检测对象是否在内存中被释放掉
  158.                 If privateDisposedBoolean = True Then
  159.                     Throw New ObjectDisposedException(privateModuleName, "这个对象已经被释放掉,你不能调用它.")
  160.                 End If
  161.                 privateConnection = New SqlConnection(privateConnectionString)
  162.                 privateCommand = New SqlCommand(SQL, privateConnection)
  163.                 privateDataSet = New DataSet
  164.                 privateSQLDataAdapter = New SqlDataAdapter(privateCommand)
  165.                 If TableName = Nothing Then
  166.                     privateSQLDataAdapter.Fill(privateDataSet)
  167.                 Else
  168.                     privateSQLDataAdapter.Fill(privateDataSet, TableName)
  169.                 End If
  170.                 Return privateDataSet
  171.             Catch ExceptionObject As Exception
  172.                 LogException(ExceptionObject)
  173.                 Throw New Exception(privateExceptionMessage, ExceptionObject)
  174.             Finally
  175.                 privateConnection.Close()
  176.             End Try
  177.         End Function
  178.         
  179. #End Region
  180. #Region "runSQLDataReader - 输入SQL语句,返回一个DataReader"
  181.         '返回DataReader
  182.         'sql:查询字符串
  183.         Public Function runSQLDataReader(ByVal SQL As String) As SqlDataReader
  184.             ValidateSQLStatement(SQL)
  185.             Try
  186.                 If privateDisposedBoolean = True Then
  187.                     Throw New ObjectDisposedException(privateModuleName, "This object has already been disposed. You cannot reuse it.")
  188.                 End If
  189.                 privateConnection = New SqlConnection(privateConnectionString)
  190.                 privateCommand = New SqlCommand(SQL, privateConnection)
  191.                 privateConnection.Open()
  192.                 privateDataReader = privateCommand.ExecuteReader
  193.                 Return privateDataReader
  194.             Catch ExceptionObject As Exception
  195.                 LogException(ExceptionObject)
  196.                 privateConnection.Close()
  197.                 Throw New Exception(privateExceptionMessage, ExceptionObject)
  198.             End Try
  199.         End Function
  200. #End Region
  201. #Region "runSQL - 运行SQL语句,没有返回值。"
  202.         'sql:查询字符串
  203.         Public Function runSQLXML(ByVal SQL As String)
  204.             ValidateSQLStatement(SQL)
  205.             Try
  206.                 If privateDisposedBoolean = True Then
  207.                     Throw New ObjectDisposedException(privateModuleName, "This object has already been disposed. You cannot reuse it.")
  208.                 End If
  209.                 privateConnection = New SqlConnection(privateConnectionString)
  210.                 privateCommand = New SqlCommand(SQL, privateConnection)
  211.                 privateConnection.Open()
  212.                 privateCommand.ExecuteNonQuery()
  213.             Catch ExceptionObject As Exception
  214.                 LogException(ExceptionObject)
  215.                 Throw New Exception(privateExceptionMessage, ExceptionObject)
  216.             Finally
  217.                 privateXMLReader.Close()
  218.                 privateConnection.Close()
  219.             End Try
  220.         End Function
  221.         Public Sub runSQL(ByVal SQL As String)
  222.             ValidateSQLStatement(SQL)
  223.             Try
  224.                 If privateDisposedBoolean = True Then
  225.                     Throw New ObjectDisposedException(privateModuleName, "This object has already been disposed. You cannot reuse it.")
  226.                 End If
  227.                 privateConnection = New SqlConnection(privateConnectionString)
  228.                 privateCommand = New SqlCommand(SQL, privateConnection)
  229.                 privateConnection.Open()
  230.                 privateCommand.ExecuteNonQuery()
  231.             Catch ExceptionObject As Exception
  232.                 LogException(ExceptionObject)
  233.                 Throw New Exception(privateExceptionMessage, ExceptionObject)
  234.             Finally
  235.                 privateConnection.Close()
  236.             End Try
  237.         End Sub
  238. #End Region
  239. #Region "RunSPDataSet - SPName:不带参数的存储过程,TableName:表名,返回一个DataSet"
  240.         '返回DataSet
  241.         'SPName:不带参数的存储过程
  242.         'TableName:表名
  243.         Public Function runSPDataSet(ByVal SPName As String, Optional ByVal TableName As String = Nothing) As DataSet
  244.             ValidateSPStatement(SPName)
  245.             Dim privateUsedParameter As Parameter           '定义一个 Parameter 型对象
  246.             Dim privateParameter As SqlParameter            '定义一个 SQLParameter 型变量
  247.             '把 privateParameterList数组里的索引值赋值给usedEnumerator
  248.             Dim usedEnumerator As IEnumerator = privateParameterList.GetEnumerator()
  249.             Try
  250.                 If privateDisposedBoolean = True Then
  251.                     Throw New ObjectDisposedException(privateModuleName, "This object has already been disposed. You cannot reuse it")
  252.                 End If
  253.                 privateConnection = New SqlConnection(privateConnectionString)
  254.                 Dim privateDataSet As New DataSet
  255.                 privateCommand = New SqlCommand(SPName, privateConnection)
  256.                 privateCommand.CommandType = CommandType.StoredProcedure
  257.                 '通过enumerator在privateParameterList数组里 获得 Parameter
  258.                 Do While (usedEnumerator.MoveNext())
  259.                     privateUsedParameter = Nothing
  260.                     '从 privateParameterList 数组里得到Parameter,赋值给privateUsedParameter
  261.                     privateUsedParameter = usedEnumerator.Current
  262.                     '转化为 SQLParameter 对象
  263.                     privateParameter = ConvertParameters(privateUsedParameter)
  264.                     '把privateParameter对象,加入到privateCommand对象的参数列表中
  265.                     privateCommand.Parameters.Add(privateParameter)
  266.                 Loop
  267.                 privateSQLDataAdapter = New SqlDataAdapter(privateCommand)
  268.                 '没有表名
  269.                 If TableName = Nothing Then
  270.                     privateSQLDataAdapter.Fill(privateDataSet)
  271.                 Else
  272.                     '有表名
  273.                     privateSQLDataAdapter.Fill(privateDataSet, TableName)
  274.                 End If
  275.                 Return privateDataSet
  276.             Catch ExceptionObject As Exception
  277.                 LogException(ExceptionObject)
  278.                 Throw New Exception(privateExceptionMessage, ExceptionObject)
  279.             Finally
  280.                 privateConnection.Close()
  281.             End Try
  282.         End Function
  283. #End Region
  284. #Region "RunSPDataReader - 带参数的存储过程,返回一个DataReader"
  285.         'SPName:带参数的存储过程
  286.         Public Function runSPDataReader(ByVal SPName As String) As SqlDataReader
  287.             ValidateSPStatement(SPName)
  288.             '设置Parameter对象
  289.             Dim privateUsedParameter As Parameter           '在privateParameterList数组里返回一个Parameter
  290.             Dim privateParameter As SqlParameter
  291.             Dim usedEnumerator As IEnumerator = privateParameterList.GetEnumerator()
  292.             Try
  293.                 If privateDisposedBoolean = True Then
  294.                     Throw New ObjectDisposedException(privateModuleName, "This object has already been disposed. You cannot reuse it")
  295.                 End If
  296.                 privateConnection = New SqlConnection(privateConnectionString)
  297.                 privateCommand = New SqlCommand(SPName, privateConnection)
  298.                 privateCommand.CommandType = CommandType.StoredProcedure
  299.                 Do While (usedEnumerator.MoveNext())
  300.                     privateUsedParameter = Nothing
  301.                     privateUsedParameter = usedEnumerator.Current
  302.                     privateParameter = ConvertParameters(privateUsedParameter)
  303.                     privateCommand.Parameters.Add(privateParameter)
  304.                 Loop
  305.                 privateConnection.Open()
  306.                 privateDataReader = privateCommand.ExecuteReader
  307.                 Return privateDataReader
  308.             Catch ExceptionObject As Exception
  309.                 LogException(ExceptionObject)
  310.                 privateConnection.Close()
  311.                 Throw New Exception(privateExceptionMessage, ExceptionObject)
  312.             End Try
  313.         End Function
  314. #End Region
  315. #Region "runSPXMLReader - 返回SQLXML字符串"
  316.         '返回privateXMLString字符串
  317.         'SPName:带参数的存储过程
  318.         Public Function runSPXMLReader(ByVal SPName As String) As String
  319.             ValidateSPStatement(SPName)
  320.             Dim privateUsedParameter As Parameter
  321.             Dim privateParameter As SqlParameter
  322.             Dim usedEnumerator As IEnumerator = privateParameterList.GetEnumerator()
  323.             Dim privateXMLString As String
  324.             Try
  325.                 If privateDisposedBoolean = True Then
  326.                     Throw New ObjectDisposedException(privateModuleName, "This object has already been disposed. You cannot reuse it")
  327.                 End If
  328.                 privateConnection = New SqlConnection(privateConnectionString)
  329.                 privateCommand = New SqlCommand(SPName, privateConnection)
  330.                 privateCommand.CommandType = CommandType.StoredProcedure
  331.                 Do While (usedEnumerator.MoveNext())
  332.                     privateUsedParameter = Nothing
  333.                     privateUsedParameter = usedEnumerator.Current
  334.                     privateParameter = ConvertParameters(privateUsedParameter)
  335.                     privateCommand.Parameters.Add(privateParameter)
  336.                 Loop
  337.                 privateConnection.Open()
  338.                 privateXMLReader = privateCommand.ExecuteXmlReader
  339.                 Do Until privateXMLReader.Read = False
  340.                     privateXMLString += privateXMLReader.ReadOuterXml & "<BR>"
  341.                 Loop
  342.                 Return privateXMLString
  343.             Catch ExceptionObject As Exception
  344.                 LogException(ExceptionObject)
  345.                 Throw New Exception(privateExceptionMessage, ExceptionObject)
  346.             Finally
  347.                 privateXMLReader.Close()
  348.                 privateConnection.Close()
  349.             End Try
  350.         End Function
  351. #End Region
  352. #Region "runSPOutput - 带参数的存储过程返回ArrayList数组对象"
  353.         Public Function runSPOutput(ByVal SPName As String) As ArrayList
  354.             ValidateSPStatement(SPName)
  355.             Dim privateUsedParameter As Parameter
  356.             Dim privateParameter As SqlParameter
  357.             Dim usedEnumerator As IEnumerator = privateParameterList.GetEnumerator()
  358.             Dim outputParameters As New ArrayList
  359.             Dim privateParameterOut As SqlParameter
  360.             If privateDisposedBoolean = True Then
  361.                 Throw New ObjectDisposedException(privateModuleName, "This object has already been disposed. You cannot reuse it")
  362.             End If
  363.             privateConnection = New SqlConnection(privateConnectionString)
  364.             privateCommand = New SqlCommand(SPName, privateConnection)
  365.             privateCommand.CommandType = CommandType.StoredProcedure
  366.             Do While (usedEnumerator.MoveNext())
  367.                 privateUsedParameter = Nothing
  368.                 privateUsedParameter = usedEnumerator.Current
  369.                 privateParameter = ConvertParameters(privateUsedParameter)
  370.                 privateCommand.Parameters.Add(privateParameter)
  371.             Loop
  372.             privateConnection.Open()
  373.             privateCommand.ExecuteNonQuery()
  374.             '通过循环取出存储过程的输出参数,赋给outputParameters数组对象
  375.             For Each privateParameterOut In privateCommand.Parameters
  376.                 If privateParameterOut.Direction = ParameterDirection.Output Or privateParameterOut.Direction = ParameterDirection.ReturnValue Then
  377.                     '存储过程返回输出参数,赋值给outputParameters数组对象
  378.                     outputParameters.Add(privateParameterOut.Value)
  379.                 End If
  380.             Next
  381.             '返回数组
  382.             Return outputParameters
  383.             privateConnection.Close()
  384.         End Function
  385. #End Region
  386. #End Region
  387. #Region "更新数据库"
  388.         Public Function updatedataset(ByVal str As String, ByVal tablename As String, ByVal dsupdate As DataSet, ByVal rowstate As DataRowState) As Boolean
  389.             Dim susses As New Boolean
  390.             privateConnection = New SqlConnection(privateConnectionString)
  391.             Try
  392.                 '检测对象是否在内存中被释放掉
  393.                 If privateDisposedBoolean = True Then
  394.                     Throw New ObjectDisposedException(privateModuleName, "这个对象已经被释放掉,你不能调用它.")
  395.                 End If
  396.                 privateCommand = New SqlCommand(str, privateConnection)
  397.                 privateSQLDataAdapter = New SqlDataAdapter(privateCommand)
  398.                 Dim dschange As DataSet
  399.                 If dsupdate.HasChanges(DataRowState.Added) Then
  400.                     dschange = dsupdate.GetChanges(DataRowState.Added)
  401.                     If dschange.HasErrors() Then
  402.                         dsupdate.RejectChanges()
  403.                         susses = False
  404.                     Else
  405.                         Dim scb As SqlCommandBuilder = New SqlCommandBuilder(privateSQLDataAdapter)
  406.                         privateSQLDataAdapter.Update(dschange, tablename)
  407.                         dsupdate.AcceptChanges()
  408.                         susses = True
  409.                     End If
  410.                 End If
  411.                 If dsupdate.HasChanges(DataRowState.Modified) Then
  412.                     dschange = dsupdate.GetChanges(DataRowState.Modified)
  413.                     If dschange.HasErrors() Then
  414.                         dsupdate.RejectChanges()
  415.                         susses = False
  416.                     Else
  417.                         Dim scb As SqlCommandBuilder = New SqlCommandBuilder(privateSQLDataAdapter)
  418.                         privateSQLDataAdapter.Update(dschange, tablename)
  419.                         dsupdate.AcceptChanges()
  420.                         susses = True
  421.                     End If
  422.                 End If
  423.                 If dsupdate.HasChanges(DataRowState.Deleted) Then
  424.                     dschange = dsupdate.GetChanges(DataRowState.Deleted)
  425.                     If dschange.HasErrors() Then
  426.                         dsupdate.RejectChanges()
  427.                         susses = False
  428.                     Else
  429.                         Dim scb As SqlCommandBuilder = New SqlCommandBuilder(privateSQLDataAdapter)
  430.                         privateSQLDataAdapter.Update(dschange, tablename)
  431.                         dsupdate.AcceptChanges()
  432.                         susses = True
  433.                     End If
  434.                 End If
  435.                 Return susses
  436.             Catch ExceptionObject As Exception
  437.                 LogException(ExceptionObject)
  438.                 Throw New Exception(privateExceptionMessage, ExceptionObject)
  439.             Finally
  440.                 privateConnection.Close()
  441.             End Try
  442.         End Function
  443. #End Region
  444. #Region "参数处理方法"
  445.         Public Sub AddParameter(ByVal ParameterName As String, _
  446.                                 Optional ByVal Value As Object = Nothing, _
  447.                                 Optional ByVal SQLType As SQLDataType = Nothing, _
  448.                                 Optional ByVal Size As Integer = Nothing, _
  449.                                 Optional ByVal Direction As ParameterDirection = ParameterDirection.Input)
  450.             Dim buildDataType As SqlDbType
  451.             Dim buildParameter As Parameter = Nothing
  452.             Select Case SQLType
  453.                 Case SQLDataType.SQLString
  454.                     buildDataType = SqlDbType.VarChar
  455.                 Case SQLDataType.SQLChar
  456.                     buildDataType = SqlDbType.Char
  457.                 Case SQLDataType.SQLInteger
  458.                     buildDataType = SqlDbType.Int
  459.                 Case SQLDataType.SQLBit
  460.                     buildDataType = SqlDbType.Bit
  461.                 Case SQLDataType.SQLDateTime
  462.                     buildDataType = SqlDbType.DateTime
  463.                 Case SQLDataType.SQLDecimal
  464.                     buildDataType = SqlDbType.Decimal
  465.                 Case SQLDataType.SQLMoney
  466.                     buildDataType = SqlDbType.Money
  467.                 Case SQLDataType.SQLImage
  468.                     buildDataType = SqlDbType.Image
  469.             End Select
  470.             buildParameter = New Parameter(ParameterName, Value, buildDataType, Size, Direction)
  471.             privateParameterList.Add(buildParameter)
  472.         End Sub
  473.         '参数类
  474.         Public Class Parameter
  475.             Public ParameterName As String
  476.             Public ParameterValue As Object
  477.             Public ParameterDataType As SQLDataType
  478.             Public ParameterSize As Integer
  479.             Public ParameterDirectionUsed As ParameterDirection
  480.             Sub New(ByVal passedParameterName As String, _
  481.                     Optional ByVal passedValue As Object = Nothing, _
  482.                     Optional ByVal passedSQLType As SQLDataType = Nothing, _
  483.                     Optional ByVal passedSize As Integer = Nothing, _
  484.                     Optional ByVal passedDirection As ParameterDirection = ParameterDirection.Input)
  485.                 ParameterName = passedParameterName
  486.                 ParameterValue = passedValue
  487.                 ParameterDataType = passedSQLType
  488.                 ParameterSize = passedSize
  489.                 ParameterDirectionUsed = passedDirection
  490.             End Sub
  491.         End Class
  492.         '转化参数函数
  493.         Private Function ConvertParameters(ByVal passedParameter As Parameter) As SqlParameter
  494.             Dim returnSQLParameter As SqlParameter = New SqlParameter
  495.             returnSQLParameter.ParameterName = passedParameter.ParameterName
  496.             returnSQLParameter.Value = passedParameter.ParameterValue
  497.             returnSQLParameter.SqlDbType = passedParameter.ParameterDataType
  498.             returnSQLParameter.Size = passedParameter.ParameterSize
  499.             returnSQLParameter.Direction = passedParameter.ParameterDirectionUsed
  500.             Return returnSQLParameter
  501.         End Function
  502.         '清除privateParameterList数组
  503.         Public Sub ClearParameters()
  504.             Try
  505.                 privateParameterList.Clear()
  506.             Catch parameterException As Exception
  507.                 Throw New Exception(privateExceptionMessage & " Parameter List did not clear", parameterException)
  508.             End Try
  509.         End Sub
  510. #End Region
  511. #Region "异常事件,并写入事件日志!"
  512.         Private Sub LogException(ByRef ExceptionObject As Exception)
  513.             Dim EventLogMessage As String
  514.             Try
  515.                 EventLogMessage = "An error occured in the following module: " & privateModuleName & _
  516.                                   " The Source was: " & ExceptionObject.Source & vbCrLf & _
  517.                                   " With the Message: " & ExceptionObject.Message & vbCrLf & _
  518.                                   " Stack Tace: " & ExceptionObject.StackTrace & vbCrLf & _
  519.                                   " Target Site: " & ExceptionObject.TargetSite.ToString
  520.                 Dim localEventLog As New EventLog("Application")
  521.                 localEventLog.WriteEntry(privateModuleName, EventLogMessage, EventLogEntryType.Error, 55)
  522.             Catch EventLogException As Exception
  523.                 Throw New Exception(privateExceptionMessage & " - EventLog Error: " & EventLogException.Message, EventLogException)
  524.             End Try
  525.         End Sub
  526. #End Region
  527. #Region "减测字符串长度"
  528.         Private Sub ValidateSQLStatement(ByRef SQLStatement As String)
  529.             'SQL 语句不小于10个字节 ( "Select * form x" )
  530.             If Len(SQLStatement) < 10 Then
  531.                 Throw New Exception(privateExceptionMessage & " The SQL Statement must be provided and at least 10 characters long")
  532.             End If
  533.         End Sub
  534.         Private Sub ValidateSPStatement(ByRef SQLStatement As String)
  535.             '存储过程 名称不小于10个字节 ( "Select * form x" )
  536.             If Len(SQLStatement) < 2 Then
  537.                 Throw New Exception(privateExceptionMessage & " The Stored Procedure must be provided and at least 2 characters long")
  538.             End If
  539.         End Sub
  540. #End Region
  541. #Region "释放资源"
  542.         Public Overloads Sub Dispose()
  543.             If privateDisposedBoolean = False Then
  544.                 Try
  545.                     privateConnection.Dispose()
  546.                 Catch
  547.                 Finally
  548.                     MyBase.Dispose()
  549.                     GC.SuppressFinalize(Me)
  550.                     privateDisposedBoolean = True
  551.                 End Try
  552.             End If
  553.         End Sub
  554. #End Region
  555. #Region "返回发生变化的记录集"
  556.         '返回发生变化的记录集.
  557.         Public Sub returndatachange(ByVal returndataset As Data.DataSet)
  558.             ' Dim datachange As Data.DataSet
  559.             Dim dtable As Data.DataTable
  560.             Dim drow As Data.DataRow
  561.             Dim dcolumn As Data.DataColumn
  562.             Try
  563.                 '更新数据-新增
  564.                 If returndataset.HasChanges(Data.DataRowState.Added) Then
  565.                     privateDataSet = returndataset.GetChanges(Data.DataRowState.Added)
  566.                     privateSQLDataAdapter.Update(privateDataSet)
  567.                     privateDataSet.AcceptChanges()
  568.                     'Return datachange
  569.                 End If
  570.                 '更新数据-修改
  571.                 If returndataset.HasChanges(Data.DataRowState.Modified) Then
  572.                     privateDataSet = returndataset.GetChanges(Data.DataRowState.Modified)
  573.                     privateSQLDataAdapter.Update(privateDataSet)
  574.                     privateDataSet.AcceptChanges()
  575.                     'Return datachange
  576.                 End If
  577.                 '更新数据-删除
  578.                 If returndataset.HasChanges(Data.DataRowState.Deleted) Then
  579.                     privateDataSet = returndataset.GetChanges(Data.DataRowState.Deleted)
  580.                     privateSQLDataAdapter.Update(privateDataSet)
  581.                     privateDataSet.AcceptChanges()
  582.                     'Return datachange
  583.                 End If
  584.             Catch Exceptionobject As Exception
  585.                 'DataSet是否有错误
  586.                 If returndataset.HasErrors Then
  587.                     '遍历每一个表
  588.                     For Each dtable In returndataset.Tables
  589.                         'Datatable是否有错误
  590.                         If dtable.HasErrors Then
  591.                             '遍历每一行
  592.                             For Each drow In dtable.Rows
  593.                                 'DataRow是否有错误
  594.                                 If drow.HasErrors Then
  595.                                     'MessageBox.Show("记录:", drow.RowError)
  596.                                     '遍历每一字段,找出DataColumn是否有错误
  597.                                     For Each dcolumn In drow.GetColumnsInError
  598.                                         ' MessageBox.Show(dcolumn.ColumnName)
  599.                                     Next
  600.                                     '清除行错误
  601.                                     drow.ClearErrors()
  602.                                     '取消发生错误的DataRow的更新.
  603.                                     drow.RejectChanges()
  604.                                 End If
  605.                             Next
  606.                         End If
  607.                     Next
  608.                 End If
  609.             End Try
  610.         End Sub
  611. #End Region
  612. #Region "属性数据库类"
  613.         '功能:按SQL语句读取记录集
  614.         '函数:SetDataSetFill
  615.         '参数:String型strSQL----要选取记录集的SQL语句
  616.         '返回:Boolean型     True--成功    False--失败
  617.         '说明:如果数据集读取成功,则将记录集存储于myDataSet中
  618.         '
  619.         Public Overridable Function SetDataSetFill(ByVal p_strSQL As String) As Boolean
  620.             Try
  621.                 If Not privateSQLDataAdapter Is Nothing Then privateSQLDataAdapter = Nothing
  622.                 privateSQLDataAdapter = New SqlDataAdapter(p_strSQL, privateConnection)
  623.                 MyClass.privateDataSet.Tables.Clear()
  624.                 privateSQLDataAdapter.Fill(privateDataSet)
  625.             Catch ex As SqlException
  626.                 Return False
  627.             End Try
  628.             Return True
  629.         End Function
  630.         '功能:取得记录行数
  631.         '函数:GetRecordCount
  632.         '参数:无
  633.         '返回:Integer 记录数目
  634.         '说明:在调用此方法前,应先调用SetDataSetFill
  635.         '
  636.         Public Overloads Function GetRecordCount() As Integer
  637.             Return MyClass.privateDataSet.Tables(0).Rows.Count()
  638.         End Function
  639.         '
  640.         '功能:取得记录行数
  641.         '函数:GetRecordCount
  642.         '参数:String型strSQL语句
  643.         '返回:Integer 记录数目
  644.         '说明:在调用此方法前,不需要先调用SetDataSetFill,此函数直接将参数传给SetDataSetFill
  645.         '
  646.         Public Overloads Function GetRecordCount(ByVal strSql As String) As Integer
  647.             If MyClass.SetDataSetFill(strSql) Then
  648.                 Return MyClass.GetRecordCount()
  649.             Else
  650.                 Return 0
  651.             End If
  652.         End Function
  653.         '
  654.         '功能:取得字段数目
  655.         '函数:GetFieldCount
  656.         '参数:无
  657.         '返回:Integer 字段数目
  658.         '说明:在调用此方法前,要先调用SetDataSetFill
  659.         '
  660.         Public Overridable Function GetFieldCount() As Integer
  661.             Return MyClass.privateDataSet.Tables(0).Columns.Count()
  662.         End Function
  663.         '
  664.         '功能:取得字段数目
  665.         '函数:GetFieldCount
  666.         '参数:String型StrSQL
  667.         '返回:Integer 字段数目
  668.         '说明:在调用此方法前,不需要先调用SetDataSetFill,此函数直接将参数传给SetDataSetFill
  669.         '
  670.         Public Overridable Function GetFieldCount(ByVal strSQL As String) As Integer
  671.             MyClass.SetDataSetFill(strSQL)
  672.             Return MyClass.privateDataSet.Tables(0).Columns.Count()
  673.         End Function
  674.         '
  675.         '功能:取得单元格数据库
  676.         '参数:Short r--第r行 , short c---第几列
  677.         '返回:String型单元数据
  678.         '说明:在调用此方法前,要先调用SetDataSetFill
  679.         Public Overridable Function GetTabValue(ByVal r As Short, ByVal c As Short) As String
  680.             Return Convert.ToString(MyClass.privateDataSet.Tables(0).Rows(r)(c))
  681.         End Function
  682.         '
  683.         '功能:取得单元格数据库
  684.         '参数:string strSqL--SQL语句,Short r--第r行 , short c---第几列
  685.         '返回:String型单元数据
  686.         '说明:在调用此方法前,不要先调用SetDataSetFill
  687.         Public Overridable Function GetTabValue(ByVal strSqL As String, ByVal r As Short, ByVal c As Short) As String
  688.             MyClass.SetDataSetFill(strSqL)
  689.             On Error GoTo Err
  690.             Return MyClass.privateDataSet.Tables(0).Rows(r)(c)
  691. Err:
  692.             Return ""
  693.         End Function
  694. #End Region
  695.     End Class
  696. End Namespace