2013-04-25 70 views
2

我正在寻找一种在使用剃须刀发出任何数据库指令时获取SQL返回码的方法。我还没有找到任何例子。返回代码是否与结果集一起返回? 例如,用Razor SQL返回代码

var products = db.Query("SELECT * FROM products"); 

会给你的结果集,但如果有一个数据库错误,我能想到的唯一办法显示错误是通过try catch块。

回答

0

您可以检查此:

Get return value from stored procedure

var returnCode = new SqlParameter(); 
returnCode.ParameterName = "@ReturnCode"; 
returnCode.SqlDbType = SqlDbType.Int; 
returnCode.Direction = ParameterDirection.Output; 

// assign the return code to the new output parameter and pass it to the sp 
var data = _context.Database.SqlQuery<Item>("exec @ReturnCode = spItemData @Code, @StatusLog OUT", returnCode, code, outParam);