7

这是从 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");

任何人:在提供的示例中的上述代码中,我收到一个错误,“无法解析 .Execute”——指的是cnn.Execute. 我查看了连接对象,没有执行的方法。Dapper 显然运作良好,那我做错了什么?

4

2 回答 2

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 );
    }
}

备注

  1. 参数不需要@ 符号;dapper 会为你处理。
  2. 一定要使用命名参数;请参阅指定commandType:参数的更新的 connection.Execute 方法。这样做是为了可以从方法调用中省略可选参数。
于 2012-09-21T16:11:26.483 回答
7

“无法解决。执行”

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

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

于 2012-09-24T10:05:46.547 回答