Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dapper-Dot-Net Example Code

Tags:

dapper

sample

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?

like image 518
Kevin Earley Avatar asked Sep 21 '12 13:09

Kevin Earley


2 Answers

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.
like image 159
Metro Smurf Avatar answered Sep 30 '22 10:09

Metro Smurf


"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

like image 36
Sam Saffron Avatar answered Sep 30 '22 12:09

Sam Saffron