Question

Regarding the new version of Microsoft Enterprise Libarary 6 , they have a method called ExecuteSprocAccessor (which should return Ienumerable<T> when execute SP) which all its overload are :

enter image description here

Usage example :

/*1*/   [Description("Return data as a sequence of objects using a stored procedure")]
/*2*/           static void ReadDataAsObjects()
/*3*/           {
/*4*/               // Create an object array and populate it with the required parameter values
/*5*/               object[] paramArray = new object[] { "%bike%" };
/*6*/               // Create and execute a sproc accessor that uses default parameter and outpu`t mappings
/*7*/               IEnumerable<Product> productData = defaultDB.ExecuteSprocAccessor<Product>("GetProductList", paramArray);
/*8*/               //...
/*9*/               //...
/*10*/           }

Additional Info :

The parameter adding mechanism (here) is very problematic because there is no ParameterName to value association.

all they do there is (at line 5)

object[] paramArray = new object[] { "%bike%" };

So I guess If I have more then 1 param it will look like :

object[] paramArray = new object[] { "%bike%",19,"lala"... };

which means that I'll have to know the order of sp's param input order !!!

NB

Other methods do have this sort of attaching values to name :

defaultDB.AddInParameter(sprocCmd, "state", DbType.String, "New York");

Question

Is there any way of using ExecuteSprocAccessor and still do ParameterName to value association ? (assuming I don't know the sp input param order ?

Was it helpful?

Solution

Internally an IParameterMapper is used which is a really simple interface:

public interface IParameterMapper
{
    /// <summary>
    /// 
    /// </summary>
    /// <param name="command"></param>
    /// <param name="parameterValues"></param>
    void AssignParameters(DbCommand command, object[] parameterValues);
}

It's all based on positions so there is not much that can be done with the out of the box code.

One option is to write your own Accessor that uses a custom parameter mapper that knows how to use parameter names. You can find an example of this in the thread Entlib6 DAAB - Using accessors to execute stored procedures with optional parameters?.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top