Simon,
First off, this is an ADO.NET question, so future questions like this one
should be posted in the adonet group.
[color=blue]
> Does anybody know how to change this function, that it will except the x
> parameters of x type?[/color]
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