2017-04-18 26 views
2

您好我正在研究mvc作爲我學習的一部分,所以我遇到了一種情況,我必須將數據表傳遞給sp以及數據表我必須給出一個類型爲int的輸出參數。目前我使用這個代碼傳遞輸出參數到接受數據表的參數使用dapper作爲參數

con.Execute("XXXXXXX", 
new { @LoginId = LoginId, @City= City, @RegionCode = RegionCode, @CarList= Cars, @CompCOde = COmp}, 
commandType: CommandType.StoredProcedure); 

這裏汽車是一個DataTable,所以我希望將輸出參數添加到這一點,請建議我一個解決方案。

回答

3

我將創建一個參數列表,其中一個指定爲ParameterDirection.Output,然後您將可以使用Get來讀取它。

我做一些假設用下面的例子包括一個int輸出參數(未測試)

  SqlConnection con = new SqlConnection(); 
      DynamicParameters p = new DynamicParameters(); 
      p.Add("@LoginId", LoginId, dbType: DbType.VarNumeric, direction: ParameterDirection.Input); 
      p.Add("@City", City, dbType: DbType.VarNumeric, direction: ParameterDirection.Input); 
      p.Add("@RegionCode", RegionCode, dbType: DbType.VarNumeric, direction: ParameterDirection.Input); 
      p.Add("@CarList", Cars.AsTableValuedParameter()); 
      p.Add("@SomeId", dbType: DbType.Int32, direction: ParameterDirection.Output); 
      con.Execute("XXXXXXX",p,commandType: CommandType.StoredProcedure); 

      int someId = p.Get<int>("@SomeId");