ado.net

我觉的应该先深刻理解下面的层次对象 和对象所提供的方法

下面是 我在做项目的 中写的 Database 类 (vb.net) 希望对你有点影响
Imports System
Imports System.ComponentModel
Imports System.Collections
Imports System.Diagnostics
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Namespace UDS.Components

 Public Class Database
 Implements IDisposable
   Private con As SqlConnection

   Public Function RunProc(ByVal procName As String) As Integer
     Dim cmd As SqlCommand = CreateCommand(procName, Nothing)
     cmd.ExecuteNonQuery
     Me.Close
     Return CType(cmd.Parameters("ReturnValue").Value, Integer)
   End Function

   Public Function RunProc(ByVal procName As String, ByVal prams As SqlParameter()) As Integer
     Dim cmd As SqlCommand = CreateCommand(procName, prams)
     cmd.ExecuteNonQuery
     Me.Close
     Return CType(cmd.Parameters("ReturnValue").Value, Integer)
   End Function

   Public Sub RunProc(ByVal procName As String, ByRef dataReader As SqlDataReader)
     Dim cmd As SqlCommand = CreateCommand(procName, Nothing)
     dataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
   End Sub

   Public Sub RunProc(ByVal procName As String, ByVal prams As SqlParameter(), ByRef dataReader As SqlDataReader)
     Dim cmd As SqlCommand = CreateCommand(procName, prams)
     dataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
   End Sub

   Private Function CreateCommand(ByVal procName As String, ByVal prams As SqlParameter()) As SqlCommand
     Open
     Dim cmd As SqlCommand = New SqlCommand(procName, con)
     cmd.CommandType = CommandType.StoredProcedure
     If Not (prams Is Nothing) Then
       For Each parameter As SqlParameter In prams
         cmd.Parameters.Add(parameter)
       Next
     End If
     cmd.Parameters.Add(New SqlParameter("ReturnValue", SqlDbType.Int, 4, ParameterDirection.ReturnValue, False, 0, 0, String.Empty, DataRowVersion.Default, Nothing))
     Return cmd
   End Function

   Private Sub Open()
     If con Is Nothing Then
       con = New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))
     End If
     If con.State = System.Data.ConnectionState.Closed Then
       con.Open
     End If
   End Sub

   Public Sub Close()
     If Not (con Is Nothing) Then
       con.Close
     End If
   End Sub

   Public Sub Dispose()
     If Not (con Is Nothing) Then
       con.Dispose
       con = Nothing
     End If
   End Sub

   Public Function MakeInParam(ByVal ParamName As String, ByVal DbType As SqlDbType, ByVal Size As Integer, ByVal Value As Object) As SqlParameter
     Return MakeParam(ParamName, DbType, Size, ParameterDirection.Input, Value)
   End Function

   Public Function MakeOutParam(ByVal ParamName As String, ByVal DbType As SqlDbType, ByVal Size As Integer) As SqlParameter
     Return MakeParam(ParamName, DbType, Size, ParameterDirection.Output, Nothing)
   End Function

   Public Function MakeReturnParam(ByVal ParamName As String, ByVal DbType As SqlDbType, ByVal Size As Integer) As SqlParameter
     Return MakeParam(ParamName, DbType, Size, ParameterDirection.ReturnValue, Nothing)
   End Function

   Public Function MakeParam(ByVal ParamName As String, ByVal DbType As SqlDbType, ByVal Size As Int32, ByVal Direction As ParameterDirection, ByVal Value As Object) As SqlParameter
     Dim param As SqlParameter
     If Size > 0 Then
       param = New SqlParameter(ParamName, DbType, Size)
     Else
       param = New SqlParameter(ParamName, DbType)
     End If
     param.Direction = Direction
     If Not (Direction = ParameterDirection.Output AndAlso Value Is Nothing) Then
       param.Value = Value
     End If
     Return param
   End Function
 End Class
End Namespace

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值