This is a small upgrade to the DoSearch method in the data access layer.
Instead of using the FieldType property for each parameter, I'm using db.DiscoverParameters, and instead of adding new parameters I'm setting the values of the discovered parameters.
I didn't want to use a discovery but after starting to use the Enterprise Library's Data block, I decided to give it a try.
As you can see it's very easy to use, just one line of code, and the discovery results are supposed to be cached, so the performance hit should be too hard.
Here's the updated code:
/// <summary>
/// Performs a search
/// </summary>
/// <param name="SPName">the name of the Stored Procedure to execute</param>
/// <param name="DBName">the name of the DataBase where the stored procedure is (if not the default database)</param>
/// <param name="searchParams">The search parameters including page size</param>
/// <returns>a dataset of the search results</returns>
public DataSet DoSearch(string SPName, string DBName, IshaiHachlili.MyTakeOnDotNet.Entities.SearchParameters searchParams)
{
Database db = DatabaseFactory.CreateDatabase(DBName);
DbCommand cmd = db.GetStoredProcCommand(SPName);
db.DiscoverParameters(cmd);
//Add form input search parameters for this search query
foreach (QueryParameter fld in searchParams.Parameters)
{
//db.AddInParameter(cmd, fld.FieldName, fld.FieldType, fld.FieldValue);
cmd.Parameters['@' + fld.FieldName].Value = fld.FieldValue;
}
//Add common parameters (all search stored procedures should have this parameters and support paging and sorting functionality)
cmd.Parameters["@PageIndex"].Value = searchParams.PageIndex;
cmd.Parameters["@PageSize"].Value = searchParams.PageSize;
cmd.Parameters["@SortColumn"].Value = searchParams.SortColumn;
cmd.Parameters["@SortOrder"].Value = searchParams.SortOrder;
DataSet ds = db.ExecuteDataSet(cmd);
return ds;
}