日期:2008-06-24  浏览次数:20576 次

' clsDataAccessOper 该类是所有数据访问类的父类

' by YuJun

‘ www.hahaIT.com


‘ hahasoft@msn.com



Public Class clsDataAccessOper



' 当Update,Delete,Add方法操作失败返回 False 时,记录出错的信息

Public Shared ModifyErrorString As String



Private Shared Keys As New Hashtable



' 数据库连接字符串

Public Shared Property ConnectionString() As String

Get

Return SqlHelper.cnnString.Trim

End Get

Set(ByVal Value As String)

SqlHelper.cnnString = Value.Trim

End Set

End Property



' Update 不更新主键,包括联合主键

Public Shared Function Update(ByVal o As Object) As Boolean

ModifyErrorString = ""

Try

If CType(SqlHelper.ExecuteNonQuery(SqlHelper.cnnString, CommandType.Text, SQLBuilder.Exists(o)), Int64) = 0 Then

Throw New Exception("该记录不存在!")

End If

Catch ex As Exception

Throw ex

End Try



Try

SqlHelper.ExecuteNonQuery(SqlHelper.cnnString, CommandType.Text, SQLBuilder.Update(o))

Catch ex As Exception

ModifyErrorString = ex.Message

Return False

End Try

Return True

End Function



' Delete 将忽略

Public Shared Function Delete(ByVal o As Object) As Boolean

ModifyErrorString = ""

Try

SqlHelper.ExecuteNonQuery(SqlHelper.cnnString, CommandType.Text, SQLBuilder.Delete(o))

Catch ex As Exception

ModifyErrorString = ex.Message

Return False

End Try

Return True

End Function



' Add 方法将忽略自动增加值的主键

Public Shared Function Add(ByVal o As Object) As Boolean

ModifyErrorString = ""

Try

SqlHelper.ExecuteNonQuery(SqlHelper.cnnString, CommandType.Text, SQLBuilder.Add(o))

Catch ex As Exception

ModifyErrorString = ex.Message

Return False

End Try

Return True

End Function



' 通用数据库查询方法

' 重载方法用于明确指定要操作的数据库表名称

' 否则会以 ReturnType 的类型描述得到要操作的数据库表的名称 eg: ReturnType="clsRooms" ,得道 TableName="tbl_Rooms"



' 该查询方法将查询条件添加到 Keys(HashTable) 中,然后调用 Select 方法返回 对象的集合

' 当Keys包含特殊键时,将要处理的是复杂类型的查询,见 SQLBuilder 的 ComplexSQL 说明

' 该方法可以拓展数据访问类的固定查询方法



Public Overloads Shared Function [Select](ByVal ReturnType As Type) As ArrayList

Dim tableName As String

tableName = ReturnType.Name

Dim i As Int16

i = tableName.IndexOf("cls") + 3

tableName = "tbl_" & tableName.Substring(i, tableName.Length - i)

Return [Select](ReturnType, tableName)

End Function



Public Overloads Shared Function [Select](ByVal ReturnType As Type, ByVal TableName As String) As ArrayList

Dim alOut As New ArrayList



Dim dsDB As New Data.DataSet

dsDB.ReadXML(clsPersistant.DBConfigPath)