DAAB - Access output params when using CreateSprocAccessor

Topics: Data Access Application Block
Apr 29, 2010 at 3:58 AM

Hi,

We've been using the following with no issues (stored proc with one input param):

var accessor = database.CreateSprocAccessor<MobileAlert>("usp_ProcessMobileRegistration", mapper, registerMapper); 
return accessor.Execute(gln);

However, we now need to be able to add and access two output parameters. We've create our own IParameterMapper

 and added the two output params but have no idea how we actually access them. Would someone be able to shed some light on this?

thanks

justin

 

 

Apr 29, 2010 at 4:09 AM

The problem is, you need to be able to have a reference to the DbCommand object, but the CreateSproceAccessor or the ExecuteSprocAccessor doesn't have the overload which takes that parameter.  I haven't tried it, but you might be able to create your own extension method which does this.

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Apr 29, 2010 at 6:27 AM

Thanks for the quick response.

Regards

justin

Jun 8, 2011 at 2:24 AM

@jus062 Were you able to solve this problem? I'm currently in the same position you were in. In particular all of our Insert stored procedurs have an output parameter that would contain the identity of the record that was just created.

Jun 8, 2011 at 3:31 AM

Hi,

Since you are just doing an Insert command, it would be better to use the ExecuteNonQuery rather than Accessor since the latter is specially designed for data retrieval (sequence of objects). As what Sarah mentioned, the DbCommand object from Accessor is something that is not exposed.

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Jun 30, 2011 at 11:32 AM

Hi,

I have one doubt , creating and using accessors is provided by Entlib, or even this functionality is provide by ado.net also?

Jul 1, 2011 at 5:57 AM

I guess you are referring to Entity Framework. Yes, both of them can act as a database accessor.

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Nov 5, 2011 at 11:48 AM
Edited Nov 5, 2011 at 11:50 AM

I'm facing the same issue here.

the only thing I can do is to change code on Entlib DAAB itself and then use it (its not the best solution but it works for me)

the changes are not that hard, open the solution Data.2010.sln 

here are the changes

1- DataAccessor.cs

- add a method  

 public abstract object GetParameterValue(string name);

 

- let the class implements IDisposable interface and put the Dispose method abstract too 

public abstract void Dispose();

 

2- CommandAccessor.cs

- add a property   

protected DbCommand Command { set;get;}

 

- implement the 2 abstract methods we added in the DataAccessor class 

        public override object GetParameterValue(string name)
        {
            return database.GetParameterValue(this.command, name);
        }

        public override void Dispose()
        {
            if (command != null)
            {
                if (command.Connection != null)
                    command.Connection.Dispose();
                command.Dispose();
            }

        }

3- SprocAccessor.cs

- locate the Command variable and replace it with the protected properties we created earlier, there are 2 methods Execute and BeginExecute

 

        public override IEnumerable<TResult> Execute(params object[] parameterValues)
        {
            this.Command = Database.GetStoredProcCommand(procedureName);
            parameterMapper.AssignParameters(Command, parameterValues);
            return base.Execute(Command);
        }

        public override IAsyncResult BeginExecute(AsyncCallback callback, object state, params object[] parameterValues)
        {
            GuardAsyncAllowed();

            Command = Database.GetStoredProcCommand(procedureName);
            return BeginExecute(Command, parameterMapper, callback, state, parameterValues);
        }

4-  SqlStringAccessor.cs
- same changes as the SprocAccessor.cs
        public override IEnumerable<TResult> Execute(params object[] parameterValues)
        {
            Command = Database.GetSqlStringCommand(sqlString);
            parameterMapper.AssignParameters(Command, parameterValues);
            foreach (TResult result in base.Execute(Command))
            {
                yield return result;
            }
        }

      
        public override IAsyncResult BeginExecute(AsyncCallback callback, object state, params object[] parameterValues)
        {
            GuardAsyncAllowed();
            Command = Database.GetSqlStringCommand(sqlString);
            return BeginExecute(Command, parameterMapper, callback, state, parameterValues);
        }