首页 > 开发 > 综合 > 正文

轻松搞定数据访问层[续2]

2024-07-21 02:25:37
字体:
来源:转载
供稿:网友

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

' by yujun

‘ www.hahait.com

‘ [email protected]



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)

dim xxxh as new hashtable

dim eachrow as data.datarow

for each eachrow in dsdb.tables(tablename).rows

if keys.contains(ctype(eachrow.item("name"), string).tolower.trim) then

xxxh.add(ctype(eachrow.item("dbname"), string).tolower.trim, keys(ctype(eachrow.item("name"), string).trim.tolower))

end if

next



' 检查 keys 的合法性

dim dsselect as new data.dataset

if keys.count <> xxxh.count then

keys.clear()

dim invalidfield as new exception("没有您设置的字段:")

throw invalidfield

else

keys.clear()

try

dsselect = sqlhelper.executedataset(sqlhelper.cnnstring, commandtype.text, sqlbuilder.select(xxxh, tablename))

catch ex as exception

throw ex

end try

end if



dim eachselect as data.datarow

dim fieldname as string

dim dbfieldname as string



for each eachselect in dsselect.tables(0).rows

dim newobject as object = system.activator.createinstance(returntype)

for each eachrow in dsdb.tables(tablename).rows

fieldname = ctype(eachrow.item("name"), string).trim

dbfieldname = ctype(eachrow.item("dbname"), string).trim

callbyname(newobject, fieldname, calltype.set, ctype(eachselect.item(dbfieldname), string).trim)

next

alout.add(newobject)

newobject = nothing

next

return alout

end function



public shared writeonly property selectkeys(byval keyname as string)

set(byval value as object)

keys.add(keyname.trim.tolower, value)

end set

end property



' 下面4个方法用来移动记录

' 移动记录安主键的大小顺序移动,只能对有且仅有一个主键的表操作

' 对于组合主键,返回 nothing

' 当记录移动到头或末尾时 返回 noting,当表为空时,first,last 均返回nothing

public shared function first(byval o as object) as object

return move("first", o)

end function



public shared function last(byval o as object) as object

return move("last", o)

end function



public shared function previous(byval o as object) as object

return move("previous", o)

end function



public shared function [next](byval o as object) as object

return move("next", o)

end function



' 返回一个表的主键的数量,keyname,keydbname 记录的是最后一个主键

private shared function getkey(byref keyname as string, byref keydbname as string, byval tablename as string) as int16

dim keynum as int16 = 0

dim dsdb as new dataset

dsdb.readxml(clspersistant.dbconfigpath)

dim row as data.datarow

for each row in dsdb.tables(tablename).rows

if row.item("key") = "1" then

keynum = keynum + 1

keyname = ctype(row.item("name"), string).trim

keydbname = ctype(row.item("dbname"), string).trim

exit for

end if

next

return keynum

end function



' 为 first,previous,next,last 提供通用函数

private shared function move(byval type as string, byval o as object) as object

dim movesql as string

select case type.trim.tolower

case "first"

movesql = sqlbuilder.first(o)

case "last"

movesql = sqlbuilder.last(o)

case "previous"

movesql = sqlbuilder.previous(o)

case "next"

movesql = sqlbuilder.next(o)

end select



dim typestring as string = o.gettype.tostring

dim i as int16

i = typestring.indexof("cls") + 3

typestring = "tbl_" & typestring.substring(i, typestring.length - i)

dim tablename as string = typestring



dim keyname as string

dim keydbname as string

dim tmpstring as string

if getkey(keyname, keydbname, tablename) = 1 then

keys.clear()

dim ds as new data.dataset

ds = sqlhelper.executedataset(sqlhelper.cnnstring, commandtype.text, movesql)

if ds.tables(0).rows.count = 0 then

return nothing

else

tmpstring = ctype(ds.tables(0).rows(0).item(keydbname), string).trim

keys.add(keyname.trim.tolower, tmpstring)

dim al as new arraylist

al = [select](o.gettype)

if al.count = 1 then

return al.item(0)

else

return nothing

end if

end if

else

return nothing

end if

end function



end class

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表