paulhudson Posted April 12, 2005 Posted April 12, 2005 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. Quote
Administrators PlausiblyDamp Posted April 12, 2005 Administrators Posted April 12, 2005 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.