加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

SqlCommand.ExecuteReader 无法获取 sqlserver 存储过程 OUTPUT

发布时间:2020-12-12 15:32:34 所属栏目:MsSql教程 来源:网络整理
导读:?public ListProduct GetProducts(long companyId,int pageSize,int pageIndex,out int rowCount) ??????? { ??????????? ListProduct list = new ListProduct(); ??????????? using (SqlConnection conn = new SqlConnection(SQLHelp.DBConnectionString))

?public List<Product> GetProducts(long companyId,int pageSize,int pageIndex,out int rowCount)
??????? {
??????????? List<Product> list = new List<Product>();
??????????? using (SqlConnection conn = new SqlConnection(SQLHelp.DBConnectionString))
??????????? {
??????????????? SqlCommand cmd = new SqlCommand();

??????????????? cmd.Connection = conn;
??????????????? cmd.CommandText = "proc_Care365_ProductPage";
??????????????? cmd.CommandType = CommandType.StoredProcedure;

??????????????? cmd.Parameters.Add(new SqlParameter("@CompanyId",companyId));
??????????????? cmd.Parameters.Add(new SqlParameter("@PageSize",pageSize));
??????????????? cmd.Parameters.Add(new SqlParameter("@PageIndex",pageIndex));

??????????????? SqlParameter param = new SqlParameter("@RowCount",SqlDbType.Int);
??????????????? param.Direction = ParameterDirection.Output;
??????????????? cmd.Parameters.Add(param);

??????????????? try
??????????????? {
??????????????????? conn.Open();
??????????????????? SqlDataReader reader = cmd.ExecuteReader();
??????????????????? if (reader != null && !reader.IsClosed && reader.HasRows)
??????????????????? {
??????????????????????? while (reader.Read())
??????????????????????? {
??????????????????????????? Product p = FabricateProduct(reader);
??????????????????????????? p.Tags = new ProductTageDAL().GetProductTags(p.Id);
??????????????????????????? p.Images = new ProductImageDAL().GetImagesByProductId(p.Id);

??????????????????????????? list.Add(p);
??????????????????????? }

?

??????????????????????? reader.Close();
??????????????????????? reader.Dispose();
??????????????????????? rowCount = Convert.ToInt32(cmd.Parameters["@RowCount"].Value.ToString());
??????????????????????? cmd.Dispose();
??????????????????????? conn.Close();
??????????????????????? conn.Dispose();

??????????????????????? return list;
??????????????????? }
??????????????????? else
??????????????????? {
??????????????????????? rowCount = 0;
??????????????????????? return null;
??????????????????? }
??????????????? }
??????????????? catch
??????????????? {
??????????????????? rowCount = 0;
??????????????????? return null;
??????????????? }
??????????? }
??????? }

?

这里要注意的是,在获取output类型参数的值的时候,必须要把reader关掉,否则无法获取值.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读