下面是 我在做项目的 中写的 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
ado.net
最新推荐文章于 2023-06-12 10:19:14 发布