Dot-Net

有沒有辦法用 Dapper 呼叫儲存過程?

  • May 11, 2011

我對 stackoverflow.com的Dapper Micro ORM的結果印象深刻。我正在為我的新項目考慮它,但我擔心有時我的項目需要儲存過程並且我在網上搜尋了很多但沒有找到任何儲存過程。那麼有什麼方法可以讓 Dapper 使用儲存過程呢?

請讓我知道是否有可能,否則我必須以我的方式擴展它。

在簡單的情況下,您可以執行以下操作:

var user = cnn.Query<User>("spGetUser", new {Id = 1}, 
       commandType: CommandType.StoredProcedure).First();

如果你想要更花哨的東西,你可以這樣做:

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<int>("@b");
int c = p.Get<int>("@c"); 

此外,您可以批量使用 exec,但這更笨重。

引用自:https://stackoverflow.com/questions/5962117