Dapper-Dot-Net示例代码

Kev*_*ley 7 sample dapper

这是Dapper示例中的代码切割:

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");
Run Code Online (Sandbox Code Playgroud)

任何人:在上面提供的示例代码中,我收到错误,"无法解析.Execute" - 指的是cnn.Execute.我查看连接对象,没有Execute的方法.小巧玲珑显然效果很好,所以我做错了什么?

Met*_*urf 9

我相信这应该让你解决:

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 );
    }
}
Run Code Online (Sandbox Code Playgroud)

备注:

  1. 参数不需要@符号; 小巧玲珑会为你处理.
  2. 一定要使用命名参数; 请参阅更新的connection.Execute方法,该方法指定commandType:参数.这样做是为了可以从方法调用中省略可选参数.


Sam*_*ron 7

"无法解决.执行"

这将导致您的扩展方法丢失,您是否using Dapper;在文件的顶部.

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