DataReader - Newbie

  • Thread starter Thread starter Brian
  • Start date Start date
B

Brian

I have a public function that I want to pass in a
StoredProc name and any parmaters that the proc might
need...

Here is my function...
Public Function FillDataReader(ByVal currentStoredProc As
String) As SqlDataReader


' Create Instance of Connection and Command
Object
Dim myConnection As SqlConnection = New
SqlConnection(ConfigurationSettings.AppSettings
("ConnectionString"))
'Dim myCommand As SqlCommand = New SqlCommand
("INV_SEL_CURRENT_VEHICLE", myConnection)

myConnection.Open()
Dim Command As New SqlCommand
(currentStoredProc, myConnection)
' Mark the Command as a SPROC
Command.CommandType =
CommandType.StoredProcedure
Return Command.ExecuteReader

End Function

To call the function I do this...

Public Function GetCurrentVehicle(ByVal
passedInVehicleID As Integer) As SqlDataReader
Return FillDataReader("INV_SEL_CURRENT_VEHICLE")

End Function



Any ideas how to pass 0, 1 or more parameters that any
given proc might need?


I've seen how to do it for a dataSet, but I want to do it
for a dataReader...

Thanks in advance!
 
With Command.Parameters
.Clear
.Add("@MyFirstParam", SqlDBType.Whatever).Value = SOmeValue
.Add("@MySecondParam", SqlDBType.Whatever).Value = SomeOtherValue
End With


However, you need to actually walk the datareader to get the data out of
it...

While dr.Read
'Do Something here

End While

You'll need to have a connection open the whole time you walk it as well, so
keep that in mind when you pass it in a function.

HTH,

Bill
 
Back
Top