473,606 Members | 3,113 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A function that returns an array of SqlClient.SqlPa rameter objects

I have a class that has several Subs that do DB things, some of which
require the same set of parameters to be passed to a stored procedure.
One class has 12 parameters and part of code used to create them is
shown below:

Dim SQLParam(12) As SqlClient.SqlPa rameter

SQLParam(0) = New System.Data.Sql Client.SqlParam eter
With SQLParam(0)
.ParameterName = "@pkUserIDOrigi nal"
.DbType = DbType.Int32
.Direction = ParameterDirect ion.Input
.Value = _pkUserID
End With
..
..
..

I then pass this array of SqlClient.SqlPa rameter objects to the command
to execute the stored procedure. Well, my question is "How do I create
a function that returns this array that I could call from all Subs that
need it (rather than having the same code in multiple places)?"

Jun 12 '06 #1
1 2399
RMT
I decided to have a "Read" and "Write" method in each class that has more or
less got a strong relationship with a record in the database. The read
method takes a data reader and reads the values into class variables, the
write method takes a command object and writes the class values into
parameters. I don't feel comfortable storing and manipulating parameter
collection objects or passing them around.


Public Class MyDBRecord

' Enumerate field positions for records returned

Private Enum PositionsEnum
ID = 0
Name
End Enum

' Example, an integer primary key and string

Private m_ID As Integer
Private m_Name As String

' Write values to the command object, ready for a stored procedure call.

Public Sub Write ( ByVal theCommand As SqlCommand )

theCommand.Para meters.Add("@In _ID", SqlDbType.Integ er).Value = m_ID
theCommand.Para meters(("@In_ID ").Directio n =
ParameterDirect ion.Input

theCommand.Para meters.Add("@In _Name", SqlDbType.Strin g).Value =
m_Name
theCommand.Para meters(("@In_Na me").Directio n =
ParameterDirect ion.Input

End Sub


' Read values from a data reader into class fields.

Public Sub Read ( ByVal theReader As SqlDataReader )

m_ID = theReader.GetIn t32 ( PositionsEnum.I D )
m_Name = theReader.GetSt ring ( PositionsEnum.N ame )

End Sub

End Class


"BobRoyAce" <br**@omegasoft wareinc.com> wrote in message
news:11******** **************@ m38g2000cwc.goo glegroups.com.. .
I have a class that has several Subs that do DB things, some of which
require the same set of parameters to be passed to a stored procedure.
One class has 12 parameters and part of code used to create them is
shown below:

Dim SQLParam(12) As SqlClient.SqlPa rameter

SQLParam(0) = New System.Data.Sql Client.SqlParam eter
With SQLParam(0)
.ParameterName = "@pkUserIDOrigi nal"
.DbType = DbType.Int32
.Direction = ParameterDirect ion.Input
.Value = _pkUserID
End With
.
.
.

I then pass this array of SqlClient.SqlPa rameter objects to the command
to execute the stored procedure. Well, my question is "How do I create
a function that returns this array that I could call from all Subs that
need it (rather than having the same code in multiple places)?"

Jun 12 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1795
by: simon | last post by:
Always when I need data reader in my programs, I simply have functions, which creates it for me: Dim rdr As SqlDataReader dim sql as string sql="myStoredProcedure" rdr = createDataReader(sql, False) And the functions are:
1
1226
by: Daren Hawes | last post by:
Hi, I have a class called Jobs. In that class is the function below.. Public Function GetJobTitle(ByVal Job_Title_ID As Integer, ByVal Company_ID As Integer) As DataSet ' Setup the Stored Procedure Dim SqlCommand As New SqlCommand Dim SqlConnection As New SqlConnection(DataLayer.ConnectionString) Try SqlCommand.CommandText = "dbo.DeleteJobTitle" SqlCommand.CommandType = System.Data.CommandType.StoredProcedure
2
1161
by: Eric Sabine | last post by:
Below is a sub I have which contains 4 lines of code. I would like to reduce it to 1. My problem is that it takes 3 lines to create the array to hold the sql parameter and since there's only one dimension in the array, I'd like to do something more like what's at the bottom of this post, but a lack of understanding prevents me from doing it properly. Any suggestions? Sub getUserVendorAssignment(ByVal UserName As String) ' fill the...
6
340
by: simon | last post by:
Always when I need data reader in my programs, I simply have functions, which creates it for me: Dim rdr As SqlDataReader dim sql as string sql="myStoredProcedure" rdr = createDataReader(sql, False) And the functions are:
0
8036
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7978
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8448
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
5987
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5470
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3948
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4010
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1572
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1313
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.