C#接收儲存過程輸出引數
/// <summary>
/// 獲取使用者列表
/// </summary>
/// <returns></returns>
public string GetUsersList(int pageNo, int pageSize, string key)
{
#region 引數陣列
//輸出引數
SqlParameter spOutput = new SqlParameter("@pageCount", SqlDbType.Int);
spOutput.Direction = ParameterDirection.Output;
SqlParameter[] commandParameters = new SqlParameter[] {
new SqlParameter("@key",key),// 查詢條件
new SqlParameter("@pageNo",pageNo),// 當前頁碼
new SqlParameter("@pageSize",pageSize),// 每頁記錄數
spOutput, // 總頁數
};
#endregion
List<EntityBase> list = new List<EntityBase>();
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
conn.Open();
SqlCommand cmd = new SqlCommand("Proc_Paging", conn);
cmd.Parameters.AddRange(commandParameters);
cmd.CommandType = CommandType.StoredProcedure;
using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
ModelUsers model = new ModelUsers();
model.id = Convert.ToInt32(dr["id"]);
model.username = Convert.ToString(dr["username"]);
model.password = Convert.ToString(dr["password"]);
list.Add(model);
}
}
// 輸出引數一定要放在最後執行,否則獲取值失敗
//object obj = spOutput.Value;
}
// 生成json資料
return JsonConvert.SerializeObject(new EntityDataCreate(list, Convert.ToInt32(spOutput.Value)));
}
public static decimal Cart_UserAmount(int UID)
{
decimal iRet;
SqlConnection conn = new SqlConnection(Conn_Str);
SqlCommand cmd = new SqlCommand("Cart_UserAmount", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@UID", UID);
cmd.Parameters.Add("@Amount", SqlDbType.Decimal).Direction=ParameterDirection.Output;
try
{
conn.Open();
cmd.ExecuteNonQuery();
iRet = (decimal)cmd.Parameters["@Amount"].Value;
}
catch (SqlException ex)
{
throw ex;
}
finally
{
conn.Close();
}
return iRet;
}