微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SqlCommand.ExecuteReader 无法获取 sqlserver 存储过程 OUTPUT 返回的参数值问题

 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关掉,否则无法获取值.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐