Dapper-Dot-Net Example Code

dapper sample

Question

This is code cut right from the Dapper examples:

var p = new DynamicParameters();
p.Add("@a", 11);
p.Add("@b", dbType: DbType.Int32, direction: ParameterDirection.Output);
p.Add("@c", dbType: DbType.Int32, direction: ParameterDirection.ReturnValue); 
cnn.Execute("spMagicProc", p, commandType: commandType.StoredProcedure);
int b = p.Get("@b");
int c = p.Get("@c");

Anyone: In the above code from the example provided, I get an error, "can't resolve .Execute"--referring to cnn.Execute. I look at the connection object and there is no method for Execute. Dapper obviously works well, so what am I doing wrong?

Accepted Answer

I believe this should get you fixed up:

using( var connection = new SqlConnection( connectionString ) )
{
    try
    {
        var p = new DynamicParameters();
        p.Add( "a", 11 );
        p.Add( "b", dbType: DbType.Int32, direction: ParameterDirection.Output );
        p.Add( "c", dbType: DbType.Int32, direction: ParameterDirection.ReturnValue );
        connection.Open();
        connection.Execute( "MyDamnStoredProc", p, commandType: CommandType.StoredProcedure );
        int b = p.Get<int>( "b" );
        int c = p.Get<int>( "c" );
    }
    catch( Exception ex )
    {
        Console.WriteLine( ex.Message );
    }
}

Notes:

  1. the params don't need the @ symbol; dapper will handle that for you.
  2. be sure to use named parameters; see the updated connection.Execute method that specifies the commandType: parameter. You do this so the optional parameters can be omitted from the method call.

Popular Answer

"can't resolve .Execute"

That would be cause your extension method is missing, did you using Dapper; at the top of your file.

See also: http://msdn.microsoft.com/en-us/library/bb308966.aspx#csharp3.0overview_topic3




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why