473,386 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How send SP parameters to the function, which creates dataReader

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:

Function createDataReader(ByVal sqlStr As String, Optional ByVal type As
Boolean = False) As SqlDataReader
Dim oCmd As SqlCommand
Dim myReader As SqlDataReader

oCmd = New SqlCommand(sqlStr, createConnection)
If type = False Then
oCmd.CommandType = CommandType.StoredProcedure
End If
myReader = oCmd.ExecuteReader(CommandBehavior.CloseConnection )
Return myReader
End Function

Function createConnection() As SqlConnection
Dim myConn As SqlConnection
myConn = New
SqlConnection(ConfigurationSettings.AppSettings("a ppStrConnection"))
myConn.Open()
createConnection = myConn
End Function
It works fine, but what if my stored procedure has parameters. You don't
know how many and you don't know the type of them.
Does anybody know how to change this function, that it will except the x
parameters of x type?

I think that in program you create some array type, fill it with parameters
and then send this array to function and in function create as many
parameters as array size is or something similar. Any idea?

Thank you for your help,
Simon
Nov 18 '05 #1
2 1378
Simon,

First off, this is an ADO.NET question, so future questions like this one
should be posted in the adonet group.
Does anybody know how to change this function, that it will except the x
parameters of x type?


Yes, you could change this function, but it would also be good for you to
check out the Data Access Application Block from Microsoft (link below),
they take care of this for you. Aslo, when you add params, the type is
based on the run-time type, so that is one less thing you need to worry
about.

Here is a quick & dirty solution:
---------------------

Function createDataReader(ByVal sqlStr As String, _
Optional ByVal type As Boolean =
False, _
Optional ParamNames( ) as String, _
Optional ParamValues( ) as Object, _
) As SqlDataReader
Dim oCmd As SqlCommand
Dim myReader As SqlDataReader

oCmd = New SqlCommand(sqlStr, createConnection)

If type = False Then
oCmd.CommandType = CommandType.StoredProcedure
End If

'// Overloading the function, and removing the Optional values
'// can clear up this ugly logic:
if (Not ((ParamNames is Nothing) _
xor (ParamValues is Nothing))) _
OrElse (Ubound(ParamNames) _
<> UBound(ParamValues)) Then
'// Error, ParamNames was provided without ParamValues
'// or UBound does not match
Throw New Exception( )
ElseIf Not ParamNames Is Nothing Then
For I as Integer = 0 to Ubound(ParamValues)
oCmd.Parameters.Add ParamNaems(i), ParamValues(i)
Next
End If
myReader = oCmd.ExecuteReader(CommandBehavior.CloseConnection )
Return myReader
End Function
'// Client usage:
DataReader = createDataReader("SELECT * FROM TABLE WHERE x = @intVal", _
True, _
New String() {"@intVal"}, _
New Integer() {57})

DataReader = createDataReader("SELECT * FROM TABLE WHERE s = @strVal", _
True, _
New String() {"@strVal"}, _
New String() {"Some Value})

'// Mixed Values:
Dim ParamValues(1) as Object

ParamValues(0) = CObj(56)
ParamValeus(1) = CObj(Now)

DataReader = createDataReader("SELECT * FROM TABLE" & _
" WHERE longval = @longVal AND dt =
@dateVal", _
True, _
New String() {"@longVal",
"@dateVal"}, _
ParamValues)
--------------------------

I did not test that code, so you will need to debug it.

Links:
Data Access Application Block:
http://msdn.microsoft.com/library/de...ml/daab-rm.asp

Data Tier (HIGHLY Recomended):
http://msdn.microsoft.com/asp.net/us...tml/boagag.asp
HTH,
Jeremy
Nov 18 '05 #2
Replace this :
if (Not ((ParamNames is Nothing) _
xor (ParamValues is Nothing))) _
OrElse (Ubound(ParamNames) _
<> UBound(ParamValues)) Then
'// Error, ParamNames was provided without ParamValues
'// or UBound does not match
Throw New Exception( )
ElseIf Not ParamNames Is Nothing Then
For I as Integer = 0 to Ubound(ParamValues)
oCmd.Parameters.Add ParamNaems(i), ParamValues(i)
Next
End If

With this:

if ((ParamNames is Nothing) _
xor (ParamValues is Nothing)) Then
'// Error, ParamNames was provided without ParamValues
'// or UBound does not match
Throw New Exception( )
ElseIf Not ParamNames Is Nothing Then

If Ubound(ParamNames) <> Ubound(ParamValues) Then
Throw New Exception( )

End If

For I as Integer = 0 to Ubound(ParamValues)
oCmd.Parameters.Add ParamNames(i), ParamValues(i)
Next
End If
Nov 18 '05 #3

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

Similar topics

0
by: bearclaws | last post by:
I am trying to send URL parameters from an HTML page to an XSL page. I used this script to pass parameters manually to the XSL and it works great. I just modified the "input for XSL-processor"...
6
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 =...
11
by: Andrew Thompson | last post by:
I have written a few scripts to parse the URL arguments and either list them or allow access to the value of any parameter by name. <http://www.physci.org/test/003url/index.html>...
2
by: Fatih BOY | last post by:
Hi, I want to send a report from a windows application to a web page like 'report.asp' Currently i can send it via post method with a context like local=En&Username=fatih&UserId=45&Firm=none...
4
by: VB Programmer | last post by:
I have a function that returns a datareader. The problem is that in the function's FINALLY I close the datareader and set it to nothing. But, BEFORE the Finally I "Return" the datareader (in the...
2
by: Jim Owen | last post by:
The following code does not operate properly, and neither I nor the three ..Net experts can figure out why. It's a seeming mystery. The code is simple. A SqlCommand is executed against a stored...
3
by: tinman | last post by:
Hi.... Assume Function A in an application calls Function GetSomeData in another assembly..... which then is the prefered method to return the SqlDatareader object back to Function A (and why...
8
by: Chris | last post by:
Hi, How can I send the parameter value for a stored procedure in ASP.NET 2.0 ? Dim sds_reader As New SqlDataSource sds_reader.SelectCommand = "STP_select_by_Type" the line below produces...
1
by: sxwend | last post by:
I am trying to use the following post results (http://www.thescripts.com/forum/thread189759.html) and add another requirement. I need to send the results to just the email addresses that the query...
2
by: netnatter | last post by:
I have used the following to get data from my SQL database and populate text & label boxes. I understand how to use parameters with gridview and data view but not with the reader. How can I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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...

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.