Login    
 
 
 
 
Text/HTML
  
You are here :: Blogs Saturday, May 19, 2012

Search
Note: This uses the internal blog search engine. The Google search engine is also available at the top of the page.
  
Disclaimer

Please review the site disclaimer before downloading or using content found on this site

  
Categories
  
DEVSHED Blog
As always, I welcome your comments!
Dec 9

Written by: Steve Gray
12/9/2010 4:16 PM  RssIcon

This is the class that I use to abstract the Enterprise Library DAAB. There are other classes, found here

 
Code Snippet
  1. Imports Microsoft.VisualBasic
  2. Imports System.Data.SqlClient
  3. Imports System.Data
  4. Imports Microsoft.Practices.EnterpriseLibrary.Data
  5. Imports Microsoft.Practices.EnterpriseLibrary.Data.Sql
  6. Imports System.Configuration
  7.  
  8. Public Class storedProcedure
  9.     Dim _storedProcName As String
  10.     Dim _db As String
  11.     Public commandParameters As System.Collections.Generic.List(Of commandParameter)
  12.     Public CommandTimeout As Int16
  13.     Dim cs As New Dictionary(Of String, String)
  14.  
  15.     Sub New(ByVal StoredProcName As String, ByVal db As String)
  16.         _storedProcName = StoredProcName
  17.         _db = db.ToUpper
  18.         commandParameters = New System.Collections.Generic.List(Of commandParameter)
  19.  
  20.         cs.Add("NGB01", "Data Source=ngb-sql-05;User Id=4pennyapp;Password=app4me;Initial Catalog=NGB01")
  21.         cs.Add("ELSTN", "Data Source=ngb-sql-05;User Id=4pennyapp;Password=app4me;Initial Catalog=ELSTN")
  22.         cs.Add("NGBOED", "Data Source=ngb-sql-05;User Id=4pennyapp;Password=app4me;Initial Catalog=NGBOED")
  23.         cs.Add("STONE", "Data Source=ngb-sql-05;User Id=4pennyapp;Password=app4me;Initial Catalog=STONE")
  24.         cs.Add("INTRANET2", "Data Source=ngb-sql-05;User Id=4pennyapp;Password=app4me;Initial Catalog=Intranet2")
  25.         cs.Add("TWO", "Data Source=vmGP11;User Id=4pennyapp;Password=app4me;Initial Catalog=TWO")
  26.     End Sub
  27.  
  28.     Function execute(ByVal timeout As Int32, ByRef cps As System.Collections.Generic.List(Of commandParameter)) As Int32
  29.         Dim strError As String = ""
  30.         Try
  31.  
  32.             Dim aParams As SqlParameter() = Nothing
  33.             Dim a As Int16 = 0
  34.  
  35.             Dim db As Database
  36.             db = New SqlDatabase(cs(_db))
  37.  
  38.             Dim dbCommand As Common.DbCommand = db.GetStoredProcCommand(_storedProcName)
  39.  
  40.             If timeout > 0 Then
  41.                 dbCommand.CommandTimeout = timeout
  42.             End If
  43.  
  44.             For Each cp As commandParameter In commandParameters
  45.                 Select Case cp.ParamDirection
  46.                     Case ParameterDirection.Input
  47.                         db.AddInParameter(dbCommand, cp.ParamName, cp.ParamType, cp.ParamValue)
  48.                     Case ParameterDirection.InputOutput
  49.                         'not handled
  50.                     Case ParameterDirection.Output
  51.                         db.AddOutParameter(dbCommand, cp.ParamName, cp.ParamType, cp.ParamSize)
  52.                     Case ParameterDirection.ReturnValue
  53.                         'not handled
  54.                 End Select
  55.             Next
  56.             db.AddParameter(dbCommand, "@ReturnValue", DbType.Int32, ParameterDirection.ReturnValue, String.Empty, System.Data.DataRowVersion.Default, 0)
  57.  
  58.             db.ExecuteNonQuery(dbCommand)
  59.             For Each cp As commandParameter In commandParameters
  60.                 cp.ParamValue = db.GetParameterValue(dbCommand, cp.ParamName)
  61.             Next
  62.  
  63.             Dim rcp As commandParameter
  64.             rcp = New commandParameter("@Return_Value", 0, DbType.Int16)
  65.             rcp.ParamValue = db.GetParameterValue(dbCommand, "@ReturnValue")
  66.             commandParameters.Add(rcp)
  67.  
  68.             cps = commandParameters
  69.             Return rcp.ParamValue
  70.         Catch ex As Exception
  71.             strError = "query: " & _storedProcName & "<BR>"
  72.             For Each cp As commandParameter In commandParameters
  73.                 strError += "params: " & cp.ParamName & " = " & cp.ParamValue.ToString & "<br>"
  74.             Next
  75.  
  76.             ex.Source = strError
  77.             Throw ex
  78.  
  79.         End Try
  80.  
  81.     End Function
  82.     Function execute() As Int32
  83.         Dim intTimeout As Int32 = -1
  84.         Dim cp As New System.Collections.Generic.List(Of commandParameter)
  85.         Return execute(intTimeout, cp)
  86.     End Function
  87.     Function execute(ByRef cp As System.Collections.Generic.List(Of commandParameter)) As Int32
  88.         Dim intTimeout As Int32 = -1
  89.         Return execute(intTimeout, cp)
  90.     End Function
  91.  
  92.     Function executeScalar() As Object
  93.         Dim strError As String = ""
  94.         Try
  95.  
  96.             Dim aParams As SqlParameter() = Nothing
  97.             Dim a As Int16 = 0
  98.             Dim db As Database '= DatabaseFactory.CreateDatabase(_db)
  99.             db = New SqlDatabase(cs(_db))
  100.             Dim dbCommand As Common.DbCommand = db.GetStoredProcCommand(_storedProcName)
  101.             For Each cp As commandParameter In commandParameters
  102.                 db.AddInParameter(dbCommand, cp.ParamName, cp.ParamType, cp.ParamValue)
  103.             Next
  104.  
  105.             Return db.ExecuteScalar(dbCommand)
  106.         Catch ex As Exception
  107.             strError = "query: " & _storedProcName & "<BR>"
  108.             For Each cp As commandParameter In commandParameters
  109.                 strError += "params: " & cp.ParamName & " = " & cp.ParamValue.ToString & "<br>"
  110.             Next
  111.             strError += ex.Message
  112.             Throw New Exception(strError)
  113.  
  114.         End Try
  115.  
  116.         executeScalar = Nothing
  117.     End Function
  118.     Function getReader(ByVal timeout As Int32) As SqlDataReader
  119.         Dim strError As String = ""
  120.         Try
  121.  
  122.             Dim aParams As SqlParameter() = Nothing
  123.             Dim a As Int16 = 0
  124.             Dim db As Database '= DatabaseFactory.CreateDatabase(_db)
  125.             db = New SqlDatabase(cs(_db))
  126.             Dim dbCommand As Common.DbCommand = db.GetStoredProcCommand(_storedProcName)
  127.             If timeout > 0 Then
  128.                 dbCommand.CommandTimeout = timeout
  129.             End If
  130.             For Each cp As commandParameter In commandParameters
  131.                 db.AddInParameter(dbCommand, cp.ParamName, cp.ParamType, cp.ParamValue)
  132.             Next
  133.  
  134.             Return db.ExecuteReader(dbCommand)
  135.         Catch ex As Exception
  136.             strError = "query: " & _storedProcName & "<BR>"
  137.             For Each cp As commandParameter In commandParameters
  138.                 strError += "params: " & cp.ParamName & " = " & cp.ParamValue.ToString & "<br>"
  139.             Next
  140.  
  141.             ex.Source = strError
  142.             Throw ex
  143.  
  144.         End Try
  145.         getReader = Nothing
  146.  
  147.     End Function
  148.     Function getReader() As SqlDataReader
  149.         getReader = getReader(-1)
  150.  
  151.     End Function
  152.     Function getTable() As DataTable
  153.         getTable = getTable(-1)
  154.     End Function
  155.  
  156.     Function getTable(ByVal timeout As Int32) As DataTable
  157.         Dim strError As String = ""
  158.         Try
  159.  
  160.             Dim aParams As SqlParameter() = Nothing
  161.             Dim a As Int16 = 0
  162.  
  163.             Dim db As Database
  164.             db = New SqlDatabase(cs(_db))
  165.  
  166.             Dim dbCommand As Common.DbCommand = db.GetStoredProcCommand(_storedProcName)
  167.  
  168.             'set the command timeout
  169.             If timeout > 0 Then
  170.                 dbCommand.CommandTimeout = timeout
  171.             End If
  172.  
  173.             For Each cp As commandParameter In commandParameters
  174.                 db.AddInParameter(dbCommand, cp.ParamName, cp.ParamType, cp.ParamValue)
  175.             Next
  176.  
  177.             Return db.ExecuteDataSet(dbCommand).Tables(0)
  178.         Catch ex As Exception
  179.             strError = "query: " & _storedProcName & "<BR>"
  180.             For Each cp As commandParameter In commandParameters
  181.                 If Not cp.ParamValue Is Nothing Then
  182.                     strError += "params: " & cp.ParamName & " = " & cp.ParamValue.ToString & "<br>"
  183.                 Else
  184.                     strError += "params: " & cp.ParamName & " = Nothing<br>"
  185.                 End If
  186.             Next
  187.  
  188.             ex.Source = strError
  189.             Throw ex
  190.         End Try
  191.         getTable = Nothing
  192.  
  193.     End Function
  194.  
  195. End Class

Tags:
Categories:

2 comment(s) so far...


Gravatar

Re: StoredProcedure Class

Very Nice Class I want to try it out,
Do you by any chance have the commandParameters.vb part of this also to build the class for commandParameters? I could try and rewrite it but I am afraid I may miss something.

Also, do you have an example of what should be passed to it to get it working as an example? 2 examlples would be better, one with 1 parameter and the 2nd with another 2 or more parameters.

Finally, can this pass back an RS of any type to populate say a datagrid or should I leave that to do within the program still?

Let me know and thanks so much,
Mike

By Mike on   12/27/2010 10:19 AM
Gravatar

Re: StoredProcedure Class

NP. The other classes that you'll need are here
http://devshed.us/Indexes/DataAccess.aspx

By Steve Gray on   12/27/2010 10:31 AM
As always, I welcome your comments!
  
 
 
Home | Products | Blogs | Contact Us | Links | God's Plan
Privacy Statement | Terms Of Use
 
Copyright 2011 by Devshed.us