I have a bit of code which can be found here. I use this all the time when I am developing classic ASP web application.
I recently found that I have a need for the same methodolgy was needed in a .Net application I recently started developing, so I came up with the following:
Code:
-
Imports System
-
Imports System.Data
-
Imports System.Data.SqlClient
-
-
-
Namespace ZipCM
-
-
Public Class Database
-
-
#Region "Properties"
-
Public ConnString As String
-
Public CmdType As Integer
-
Public Query As String
-
Public Values As Object
-
Public ParamNames As Object
-
Public ReturnValues As Object
-
Public FieldCount As Long
-
Public TotalRecords As Long
-
Public AffectedRows As Long
-
Public ReturnValue As Object
-
Public HasReturn As Boolean
-
Public ReturnParamName As String
-
Public ReturnDataType As Object
-
-
Private i As Integer
-
#End Region
-
-
#Region "ExecuteDataReader"
-
Public Sub ExecuteDataReader()
-
Try
-
Dim objConn As New SqlConnection(ConnString)
-
Dim objCmd As New SqlCommand(Query, objConn)
-
Dim objDA As New SqlDataAdapter()
-
Dim objDS As New DataSet()
-
If IsArray(Values) And IsArray(ParamNames) Then
-
If UBound(Values) = UBound(ParamNames) Then
-
‘Prepare our parameters
-
For Me.i = 0 To UBound(Values)
-
objCmd.Parameters.AddWithValue(ParamNames(i), Values(i))
-
Next
-
objCmd.CommandType = CmdType
-
objCmd.Connection.Open()
-
objDA.SelectCommand = objCmd
-
objDA.Fill(objDS, "Tbl")
-
objCmd = Nothing
-
objConn.Close() : objConn = Nothing
-
Dim DT As DataTable = objDS.Tables("Tbl")
-
FieldCount = DT.Columns.Count
-
TotalRecords = DT.Rows.Count
-
Dim c As Integer
-
Dim r As Integer
-
Dim tmpRetVals(FieldCount – 1, TotalRecords – 1) As Array
-
For c = 0 To FieldCount – 1
-
For r = 0 To TotalRecords – 1
-
tmpRetVals(c, r) = New String() {DT.Columns.Item(c).ColumnName, DT.Rows(r)(c).ToString()}
-
Next
-
Next
-
ReturnValues = tmpRetVals
-
-
End If
-
End If
-
Catch ex As Exception
-
-
End Try
-
End Sub
-
-
#End Region
-
-
#Region "Execute Non Data Reader"
-
-
Public Sub ExecuteNDQuery()
-
Try
-
Dim objConn As New SqlConnection(ConnString)
-
Dim objCmd As New SqlCommand(Query, objConn)
-
Dim objParam As SqlParameter
-
If IsArray(Values) And IsArray(ParamNames) Then
-
If UBound(Values) = UBound(ParamNames) Then
-
‘Prepare our parameters
-
If HasReturn Then
-
objParam = New SqlParameter(ReturnParamName, ReturnDataType)
-
objParam.Direction = ParameterDirection.Output
-
objCmd.Parameters.Add(objParam)
-
objParam = Nothing
-
End If
-
For Me.i = 0 To UBound(Values)
-
objCmd.Parameters.AddWithValue(ParamNames(i), Values(i))
-
Next
-
objCmd.CommandType = CmdType
-
objCmd.Connection.Open()
-
If HasReturn Then
-
objCmd.ExecuteNonQuery()
-
ReturnValue = objCmd.Parameters(ReturnParamName).Value
-
Else
-
objCmd.ExecuteNonQuery()
-
End If
-
objCmd.Connection.Close() : objConn = Nothing
-
-
End If
-
End If
-
Catch ex As Exception
-
-
End Try
-
End Sub
-
-
#End Region
-
-
End Class
-
End Namespace
Link to this post!












