欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net下使用DbProviderFactories的数据库操作类

程序员文章站 2024-03-31 19:55:22
封装数据库操作,并且提供事务处理。 复制代码 代码如下: 使用dbproviderfactories的数据库操作类 imports system.data imports...
封装数据库操作,并且提供事务处理。
复制代码 代码如下:

使用dbproviderfactories的数据库操作类
imports system.data
imports system.configuration
imports system.data.common

'*******************************************************************
'* page/class name:xpdbhelper.vb
'* title:使用dbproviderfactories的数据库操作类
'* description:简单三层结构数据库操作类
'* copyright:
'* company:
'* @author:owen,yuan
'* create date:2010/3/9 14:04:56
'* last modifier:
'* last modify date:
'*******************************************************************
public class xpdbhelper

'取得连接字符串中的数据库驱动提供者
private shared dbprovidername as string = configurationmanager.connectionstrings("connectionstring").providername

'取得数据库连接字符串
private shared dbconnectionstring as string = configurationmanager.connectionstrings("connectionstring").connectionstring

'数据库连接。
private connection as dbconnection

public sub new()
me.connection = createconnection(xpdbhelper.dbconnectionstring)
end sub

public sub new(byval connectionstring as string)
me.connection = createconnection(connectionstring)
end sub

'创建数据库连接
public shared function createconnection() as dbconnection
dim dbfactory as dbproviderfactory = dbproviderfactories.getfactory(dbprovidername)
dim dbconn as dbconnection = dbfactory.createconnection()
dbconn.connectionstring = xpdbhelper.dbconnectionstring
return dbconn
end function

'创建数据库连接
public shared function createconnection(byval connectionstring as string) as dbconnection
dim dbfactory as dbproviderfactory = dbproviderfactories.getfactory(dbprovidername)
dim dbconn as dbconnection = dbfactory.createconnection()
dbconn.connectionstring = connectionstring
return dbconn
end function

'根据存储过程创建dbcommand
public function getstoredproccommond(byval storedprocedure as string) as dbcommand
dim dbcommand as dbcommand = connection.createcommand()
dbcommand.commandtext = storedprocedure
dbcommand.commandtype = commandtype.storedprocedure
return dbcommand
end function

'根据sql语句创建dbcommand
public function getsqlstringcommond(byval sqlquery as string) as dbcommand
dim dbcommand as dbcommand = connection.createcommand()
dbcommand.commandtext = sqlquery
dbcommand.commandtype = commandtype.text
return dbcommand
end function

#region "增加参数"
public sub addparametercollection(byval cmd as dbcommand, byval dbparametercollection as dbparametercollection)
for each dbparameter as dbparameter in dbparametercollection
cmd.parameters.add(dbparameter)
next
end sub

public sub addoutparameter(byval cmd as dbcommand, byval parametername as string, byval dbtype as dbtype, byval size as integer)
dim dbparameter as dbparameter = cmd.createparameter()
dbparameter.dbtype = dbtype
dbparameter.parametername = parametername
dbparameter.size = size
dbparameter.direction = parameterdirection.output
cmd.parameters.add(dbparameter)
end sub


public sub addinparameter(byval cmd as dbcommand, byval parametername as string, byval dbtype as dbtype, byval value as object)
dim dbparameter as dbparameter = cmd.createparameter()
dbparameter.dbtype = dbtype
dbparameter.parametername = parametername
dbparameter.value = value
dbparameter.direction = parameterdirection.input
cmd.parameters.add(dbparameter)
end sub

public sub addreturnparameter(byval cmd as dbcommand, byval parametername as string, byval dbtype as dbtype)
dim dbparameter as dbparameter = cmd.createparameter()
dbparameter.dbtype = dbtype
dbparameter.parametername = parametername
dbparameter.direction = parameterdirection.returnvalue
cmd.parameters.add(dbparameter)
end sub

public function getparameter(byval cmd as dbcommand, byval parametername as string) as dbparameter
return cmd.parameters(parametername)
end function
#end region

#region "执行"
'执行dbcommand,返回dataset
public function executedataset(byval cmd as dbcommand) as dataset
dim dbfactory as dbproviderfactory = dbproviderfactories.getfactory(dbprovidername)
dim dbdataadapter as dbdataadapter = dbfactory.createdataadapter()
dbdataadapter.selectcommand = cmd
dim ds as dataset = new dataset()
dbdataadapter.fill(ds)
return ds
end function

'执行dbcommand,返回datatable
public function executedatatable(byval cmd as dbcommand) as datatable
dim dbfactory as dbproviderfactory = dbproviderfactories.getfactory(dbprovidername)
dim dbdataadapter as dbdataadapter = dbfactory.createdataadapter()
dbdataadapter.selectcommand = cmd
dim datatable as datatable = new datatable()
dbdataadapter.fill(datatable)
return datatable
end function

'执行dbcommand,返回dbdatareader
public function executereader(byval cmd as dbcommand) as dbdatareader
cmd.connection.open()
dim reader as dbdatareader = cmd.executereader(commandbehavior.closeconnection)
cmd.connection.close()
return reader
end function

public function executenonquery(byval cmd as dbcommand) as integer
cmd.connection.open()
dim ret as integer = cmd.executenonquery()
cmd.connection.close()
return ret
end function

public function executescalar(byval cmd as dbcommand) as object
cmd.connection.open()
dim ret as object = cmd.executescalar()
cmd.connection.close()
return ret
end function
#end region

#region "执行事务"

public function executedataset(byval cmd as dbcommand, byval t as xptransaction) as dataset
cmd.connection = t.dbconnection
cmd.transaction = t.dbtransact
dim dbfactory as dbproviderfactory = dbproviderfactories.getfactory(xpdbhelper.dbprovidername)
dim dbdataadapter as dbdataadapter = dbfactory.createdataadapter()
dbdataadapter.selectcommand = cmd
dim ds as dataset = new dataset()
dbdataadapter.fill(ds)
return ds
end function

public function executedatatable(byval cmd as dbcommand, byval t as xptransaction) as datatable
cmd.connection = t.dbconnection
cmd.transaction = t.dbtransact
dim dbfactory as dbproviderfactory = dbproviderfactories.getfactory(xpdbhelper.dbprovidername)
dim dbdataadapter as dbdataadapter = dbfactory.createdataadapter()
dbdataadapter.selectcommand = cmd
dim datatable as datatable = new datatable()
dbdataadapter.fill(datatable)
return datatable
end function

public function executereader(byval cmd as dbcommand, byval t as xptransaction) as dbdatareader
cmd.connection.close()
cmd.connection = t.dbconnection
cmd.transaction = t.dbtransact
dim reader as dbdatareader = cmd.executereader()
return reader
end function

public function executenonquery(byval cmd as dbcommand, byval t as xptransaction) as integer
cmd.connection.close()
cmd.connection = t.dbconnection
cmd.transaction = t.dbtransact
return cmd.executenonquery()
end function

public function executescalar(byval cmd as dbcommand, byval t as xptransaction) as object
cmd.connection.close()
cmd.connection = t.dbconnection
cmd.transaction = t.dbtransact
return cmd.executescalar()
end function
#end region

end class



简单三层结构数据库事务处理类
复制代码 代码如下:

imports system.data
imports system.data.common

'*******************************************************************
'* page/class name:xptransaction.vb
'* title:数据库事务处理类
'* description:简单三层结构数据库事务处理类
'* copyright:
'* company:
'* @author:owen,yuan
'* create date:2010/3/22 10:44:20
'* last modifier:
'* last modify date:
'*******************************************************************
public class xptransaction
implements idisposable

private conn as dbconnection
private dbtrans as dbtransaction

public property dbconnection() as dbconnection
get
return me.conn
end get
set(byval value as dbconnection)
me.conn = value
end set
end property

public property dbtransact() as dbtransaction
get
return me.dbtrans
end get
set(byval value as dbtransaction)
me.dbtrans = value
end set
end property

public sub new()
conn = xpdbhelper.createconnection()
conn.open()
dbtrans = conn.begintransaction()
end sub

public sub new(byval connectionstring as string)
conn = xpdbhelper.createconnection(connectionstring)
conn.open()
dbtrans = conn.begintransaction()
end sub

public sub commit()
dbtrans.commit()
me.colse()
end sub

public sub rollback()
dbtrans.rollback()
me.colse()
end sub

public sub colse()
if conn.state = connectionstate.open then
conn.close()
end if
end sub

private disposedvalue as boolean = false ' 检测冗余的调用

' idisposable
protected overridable sub dispose(byval disposing as boolean)
if not me.disposedvalue then
if disposing then
conn.close()
' todo: 释放其他状态(托管对象)。
end if
conn = nothing
dbtrans = nothing
' todo: 释放您自己的状态(非托管对象)。
' todo: 将大型字段设置为 null。
end if
me.disposedvalue = true
end sub

#region " idisposable support "
' visual basic 添加此代码是为了正确实现可处置模式。
public sub dispose() implements idisposable.dispose
' 不要更改此代码。请将清理代码放入上面的 dispose(byval disposing as boolean) 中。
dispose(true)
gc.suppressfinalize(me)
end sub
#end region

end class