[英]Return INT from T-SQL not evaluating in Published Environment
我遇到了一個問題,在該問題中,我編寫了一些可在沙箱中使用的東西,包括指向外部數據庫時無法在我發布的環境中使用的東西。
我想做的是從存儲過程中得到一個INT結果。 我嘗試過讓該過程返回INT並將其設置為OUTPUT參數,無論哪種情況都沒有運氣。 請注意,這再次適用於我自己的沙箱,但不適用於使用Visual Studio部署功能進行部署的已發布環境。
這是C#評估整數返回代碼:
var returnVal = new SqlParameter("@ReturnValue", SqlDbType.Int)
{
IsNullable = true,
Direction = ParameterDirection.ReturnValue
};
var sqlParams = new List<SqlParameter>
{
new SqlParameter("@Host", dataEntryModel.Host),
new SqlParameter("@PurchaseOrderNumberUser", dataEntryModel.PurchaseOrderNumberUser),
new SqlParameter("@PharmacyId", dataEntryModel.BatchUploadPharmacyId),
new SqlParameter("@NDCTable", table),
new SqlParameter("@LastModifiedById", dataEntryModel.LastModifiedById),
new SqlParameter("@POSourceTypeId", dataEntryModel.PurchaseOrderSourceType),
new SqlParameter("@PoId", dataEntryModel.PurchaseOrderId),
returnVal
};
using (var connection = new SqlConnection(Database.ConnectionString))
{
connection.Open();
using (var command = new SqlCommand("AdminApp._spBatchUploadNDCToPurchaseOrder"))
{
command.Connection = connection;
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddRange(sqlParams.ToArray());
// Setting command timeout to 120 second - Added by Tom Miller 10-31-14
command.CommandTimeout = 600;
command.ExecuteScalar();
returnPurchaseOrderNumber = Convert.ToInt32(command.Parameters["@ReturnValue"].Value);
}
}
return returnPurchaseOrderNumber;
我不確定為什么這在其他環境中不起作用。 我已將我的沙箱指向部署環境中使用的開發數據庫,它在運行時顯示正在評估的返回碼為0,這是不正確的。
解決經過一些測試,我完全偏離了方向。 我的數據設置正確,但是由於SQL端的體系結構不佳,引入DEV時的數據布局有所不同。 謝謝您,並為此致歉。
您正在嘗試以錯誤的方式投射結果。 嘗試這個:
int result = Convert.ToInt32(command.ExecuteScalar());
command
沒有名為@ReturnValue
的參數。 [編輯:抱歉,是的,確實是,請參閱注釋]設置一個斷點並檢查,但是它可能會評估為null
,並嘗試將null
轉換為整數。 我希望會引發異常,但是如果沒有異常,它將求值為默認整數值,即0。
如果您的存儲過程正在返回值,則使用NicoRiff的方法並將ExecuteScalar()
的結果分配給變量。 如果它正在使用輸出參數,則給它一個參數。 聲明這樣的輸出參數:
new SqlParameter("@ReturnValue", SqlDbType.Int).Direction = ParameterDirection.Output
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.