Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have two functions (GetAllAccounts and GetTeamAccounts) that call another function (GetReader) to pull data from database. See code below.

 

The function GetAllAccounts uses a stored procedure 'spAllAccounts' that does not require parameters. The second function GetTeamAccounts uses stored procedure called 'spTeamAccounts' which requires a parameter.

 

Obviously I could use two separate readers one using the parameter and one without. But I want to save on code.

 

So my question is how, within the GetReader function do I ascertain which function called it? If it is GetTeamAccounts then I can add a parameter (as shown below in red) using IF.

 

 

Function GetAllAccounts()
Dim sSelect As String
sSelect = "spAllAccounts"
Return GetReader(sSelect)
End Function

Function GetTeamAccounts(sKey As String)
Dim sSelect As String
sSelect = "spTeamAccounts"
Return GetReader(sSelect)
End Function

Function GetReader (sSQL As String) As OleDbDataReader
	Dim oConnect As New OleDbConnection(ConfigurationSettings.AppSettings("connString"))
 	Dim oCommand As New OleDbCommand(sSQL, oConnect)
oCommand.commandType = CommandType.StoredProcedure
[color=Red]	If ************** then
	dim oParam as OleDbParameter
	oParam = oCommand.Parameters.Add("@ParamID", _
	OleDbType.Integer)
	oParam.Direction = ParameterDirection.Input
	oParam.Value = sKey
end if[/color]	
Try
	oConnect.Open()
	Return oCommand.ExecuteReader(CommandBehavior.CloseConnection)
		Catch oErr As Exception
 		' be sure to close connection if error occurs
 		If oConnect.State <> ConnectionState.Closed Then
  			oConnect.Close()
 		End If
	lblErr.Text = oErr.Message & "<p/>"
End Try
End function

Hope someone can help? Thanks.

  • Administrators
Posted

Rather than pass a string identifying the stored proc to the reader why not create a version that accepts a string and a variable number of parameters? The GetAllAccounts would pass no parameters, GetTeamAccounts would be responsible for creating the correct parameters and passing them to the routine.

Something along the line of

 

Function GetReader(ByVal storedProcedure As String, ByVal ParamArray parameters() As System.Data.IDataParameter) As OleDbDataReader

and just add the passed parameters to the command object instantiated within the GetReader function.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...