SQLServer .NET database operation programming

xiaoxiao2021-03-06  21

Imports System.Data.SqlClientNamespace ReadData Public Class Database Private con As SqlClient.SqlConnection Private Sub Open () 'Open Database Connectivity If con Is Nothing Then con = New SqlConnection (ConfigurationSettings.AppSettings ( "ConnectionString")) End If If con.State = System.data.connectionsState.closed the Con. Open () endiff

End Sub Public Sub Close () Close IF NOT Con Is Nothing Then Con. Close () end if End Sub

Public Sub Dispose () confirms if the connection is closed if not con lyning kil.dispose () Con = Nothing end if End Sub

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) endiff

PARAM.DIRECTION = Direction if not (direction = parameterdirection.output and value = nothing) Then param.value = value Endiff

Return Param 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 MakeOutParam (ByVal ParamName As String, ByVal DbType As SqlDbType, ByVal Size As Integer) As SqlParameter Return MakeParam (ParamName, DbType, Size, ParameterDirection.Output, Nothing) End FunctionPublic 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

Private function createcommand (Byval prams () as sqlparameter) AS SQLCOMMAND 'confirms open connection open ()

DIM CMD As Sqlcommand = New Sqlcommand (Procname, Con) cmd.commandtype = CommandType.StoredProcedure

'Incorporation of parameters into stored procedure if not prams is nothing the dim parameter as sqlparameter for Each Parameter In Prams cmd.parameters.add (parameter) Next End End End End

'Join the parameter cmd.Parameters.Add (New Sqlparameter ("ReturnValue", SqldbType.int, 4, ParameterDirection.ReturnValue, False, 0, 0, String.empty, DataRowVersion.default, Nothing)

Return cmd end function

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

'/

' / execute stored procedure '/ ' / Stored Procedure Name '/ The parameter required for the stored procedure '/ returns stored procedure return value 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 ("ReturnValue"). Value, Integer) End Function '/ ' / Perform Store '/ ' / Storage procedure name '/ return stored procedure return value Public Sub RunProc (ByVal procName As String, ByRef dataReader As SqlDataReader) Dim cmd As SqlCommand = CreateCommand (procName, Nothing ) DataReader = cmd.executeRead (System.data.commandbehavior.closeConnection)

End Sub

'/

' / Execute Store '/ ' / > Name of the stored procedure '/ The required parameters required for the stored procedure / param> '/ required parameters stored procedure Public Function 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) 'Return (int) cmd.parameters ["ReturnValue"]. Value; End Functionend Class

End Namespace

转载请注明原文地址:https://www.9cbs.com/read-42549.html

New Post(0)