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

封装数据库操作,并且提供事务处理。


代码如下:

使用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

(0)

相关推荐

  • ASP.NET web.config中数据库连接字符串connectionStrings节的配置方法

    在ASP.NET开发的网站根目录,有一个名为web.config的文件,顾名思义,这是为整个网站进行配置的文件,其格式为XML格式.这里主要谈谈文件中的<connectionStrings>节. <connectionStrings>节是对连接到数据库的字符串进行配置,由于MS SQL Server与ASP.NET同属于微软的产品,因此是使用ASP.NET开发时首选的数据库是MS SQL Server,本文只讨论对MS SQL Server的连接字符串情况.第一种情况,本地开发时,

  • asp.net Oracle数据库访问操作类

    复制代码 代码如下: using System;using System.Collections;using System.Collections.Specialized;using System.Data;using System.Data.OracleClient;using System.Configuration;using System.Data.Common;using System.Collections.Generic; /// <summary>    /// 数据访问抽象基

  • asp.net下Oracle,SQL Server,Access万能数据库通用类

    复制代码 代码如下: using System; using System.Collections; using System.Collections.Specialized; using System.Data; using System.Data.SqlClient; using System.Data.OleDb; using System.Data.OracleClient; using System.Configuration; using System.Reflection; nam

  • asp.net 数据库的连接和datatable类

    using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.D

  • 一个ASP.NET的MYSQL的数据库操作类自己封装的

    /** * 类说明:对MYSQL数据库的操作类 */ using System; using System.Data; using MySql.Data.MySqlClient; namespace Niunan.BYLW.Utility { /// <summary>对MYSQL数据库的操作类 /// /// </summary> public class MYSQLHelper { private MySqlConnection conn = null; private MyS

  • ASP.NET封装的SQL数据库访问类

    using System; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Collections; namespace MyCorporation.DepartMent.DataBase { /// <summary> /// 通用数据库类 /// </summary> public class DataBase { private string Co

  • ASP.NET 6种常用数据库的连接方法

    1.C#连接连接Access 程序代码:     复制代码 代码如下: using   System.Data; using   System.Data.OleDb;    .. string   strConnection="Provider=Microsoft.Jet.OleDb.4.0;";     strConnection+=@"Data   Source=C:BegASPNETNorthwind.mdb"; OleDbConnection   objCo

  • ASP.NET2.0 SQL Server数据库连接详解

    Connection类有四种:SqlConnection,OleDbConnection,OdbcConnection和OracleConnection. SqlConnection类的对象连接SQL Server数据库:OracleConnection 类的对象连接Oracle数据库: OleDbConnection类的对象连接支持OLE DB的数据库,如Access:而OdbcConnection类的对象连接任何支持ODBC的数据库.与数据库的所有通讯最终都是通过Connection对象来完

  • Asp.net把图片存入数据库和读取图片的方法

    网上关于ASP.NET上传图片到数据库的资料非常多,常用的如下:存储图片类型数据有以下几种方式:1.将图片转换为二进制数组(byte[]) 复制代码 代码如下: byte[] fileData = this.FileUpload1.FileBytes; 2. 根据路径将文件转换为2进制数组 复制代码 代码如下: 代码 public byte[] returnbyte(string strpath){ // 以二进制方式读文件    FileStream fsMyfile = new FileSt

  • ASP.NET对SQLServer的通用数据库访问类

    本文模仿实现数据库访问的通用类,代码清晰,而且很实用,包括了对数据库的所有的常用的操作. /// <summary> /// 数据库访问通用类 /// </summary> public class SqlHelper { private string connectionString; /// <summary> /// 设定数据库访问字符串 /// </summary> public string ConnectionString { set { con

  • asp.net连接查询SQL数据库并把结果显示在网页上(2种方法)

    在ASP.NET中,使用C#连接SQL数据库,并使用SQL语句查询,以前从来没有接触过C#,最近用到了,摸索了两天终于运行起来了,Mark一下,不喜勿喷 有两种方法:(说的是第一种方法不安全,我也不清楚^_^) 第一种方法: 复制代码 代码如下: //建立ASP.NET Web 应用程序,直接在Page_load函数中加入一下代码,貌似就可以用了 public void Page_Load(object sender, EventArgs e) { using (SqlConnection co

  • ASP.NET数据库操作类实例

    本文实例讲述了ASP.NET数据库操作类.分享给大家供大家参考,具体如下: using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using Syst

  • asp.net 数据库连接类代码(SQL)

    复制代码 代码如下: public class SqlOperation { #region 属性 /// <summary> /// 保存在Web.config中的连接字符串 /// </summary> protected static string connectionstring = System.Configuration.ConfigurationManager.ConnectionStrings["hao"].ConnectionString; /

随机推荐