这段时间在做一个价格平台的项目时候,同事让我写一个存储过程。该存储过程是根据查询条件得出一组新数据,并且返回该组数据的总条数,此处的存储过程我用到了分页,其中主要知识点和难点是之前做项目的时候没有用到过存储过程输出参数,更没有在C#调用存储过程的时候得到输出参数的值,因此在网上搜了一会很快找到答案。

此处难点一:输出参数的定义

    -- Add the parameters for the stored procedure here
@title varchar(100),
@pageindex int,
@pagesize int,
@result_value int out -- 输入出参数

难点二:在数据库中执行(exec)存储过程输出参数怎么用

--首先定义输出参数
DECLARE @result_value int
exec [searchdata] '三',2,2,@result_value output
--搜索输出参数值
SELECT @result_value

难点三:改造数据库操作类

在下面的操作方法参数中定义了一个out类型的输出参数,目的是在调用下面的方法的时候给数据总条数赋值。

 /// <summary>
/// 执行存储过程取得数据
/// </summary>
/// <param name="storeName">存储过程名字</param>
/// <param name="parameters">存储过程参数</param>
/// <returns></returns>
public static DataTable ExecuteStoredPro(string storeName, out int resultcount, string title, int pageindex, int pagesize)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = storeName;
SqlParameter[] para ={
new SqlParameter("@title",SqlDbType.Int),
new SqlParameter("@pageindex",SqlDbType.Int),
new SqlParameter("@pagesize",SqlDbType.Int),
new SqlParameter("@result_value",SqlDbType.Int) }; para[].Value = title;
para[].Value = pageindex;
para[].Value = pagesize;
para[].Direction = ParameterDirection.Output; //设定参数的输出方向 cmd.Parameters.AddRange(para);// 将参数加入命令对象
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt); resultcount = Convert.ToInt32(cmd.Parameters[].Value); return dt; }
} }

总结:技术难点分解完成顺便把存储过程公布一下

ALTER PROCEDURE [dbo].[searchdata]
-- Add the parameters for the stored procedure here
@title varchar(100),
@pageindex int,
@pagesize int,
@result_value int out -- 输入出参数
AS
BEGIN
declare @resultcount int
--创建临时数据库表开始
if exists(select * from dbo.sysobjects where id = object_id(N'[dbo].[#tmp_linshi]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
begin
drop table #tmp_linshi
end
else
begin
CREATE TABLE #tmp_linshi (id int identity(1,1),nid int,channel_id int,company_id int,title varchar(150),retail_price nvarchar(20))
end
--创建临时数据库表结束
--超市商品数据查询
insert into #tmp_linshi (nid,channel_id,company_id,title,retail_price) select id,channel_id,company_id,title,retail_price from dbo.eazy_life where title like '%'+@title+'%'
--家用电气数据查询
insert into #tmp_linshi (nid,channel_id,company_id,title,retail_price) select id,channel_id,company_id,title,retail_price from dbo.eazy_appliances where title like '%'+@title+'%'
--汽车数据查询
insert into #tmp_linshi (nid,channel_id,company_id,title,retail_price) select id,channel_id,company_id,title,retail_price from dbo.eazy_car where title like '%'+@title+'%'
--农贸数据查询
insert into #tmp_linshi (nid,channel_id,company_id,title,retail_price) select id,channel_id,company_id,title,price from dbo.eazy_farm where title like '%'+@title+'%'
--医药数据查询
insert into #tmp_linshi (nid,channel_id,company_id,title,retail_price) select id,channel_id,company_id,title,retail_price from dbo.eazy_medicine where title like '%'+@title+'%'
--客运数据查询
insert into #tmp_linshi (nid,channel_id,company_id,title,retail_price) select id,channel_id,company_id,title,price from dbo.eazy_amtrack where title like '%'+@title+'%'
--景点数据查询
insert into #tmp_linshi (nid,channel_id,company_id,title,retail_price) select id,channel_id,company_id,title,price from dbo.eazy_scenic where title like '%'+@title+'%'
--旅行社数据查询
insert into #tmp_linshi (nid,channel_id,company_id,title,retail_price) select id,channel_id,company_id,title,price from dbo.eazy_travel where title like '%'+@title+'%'
--酒店住宿数据查询
insert into #tmp_linshi (nid,channel_id,company_id,title,retail_price) select id,channel_id,company_id,title,price from dbo.eazy_hotel where title like '%'+@title+'%'
--文化娱乐数据查询
insert into #tmp_linshi (nid,channel_id,company_id,title,retail_price) select id,channel_id,company_id,title,price from dbo.eazy_culture where title like '%'+@title+'%'
--餐饮美食数据查询
insert into #tmp_linshi (nid,channel_id,company_id,title,retail_price) select id,channel_id,company_id,title,price from dbo.eazy_food where title like '%'+@title+'%'
--电力电缆数据查询
insert into #tmp_linshi (nid,channel_id,company_id,title,retail_price) select id,channel_id,company_id,title,retail_price from dbo.eazy_electric where title like '%'+@title+'%'
select @result_value=COUNT(*) from #tmp_linshi
select * from (select *,ROW_NUMBER() over(order by id) rownum from #tmp_linshi) t where t.rownum>=(@pageindex-1)*@pagesize+1 and t.rownum<=@pageindex*@pagesize
END

asp.net C#操作存储过程读取存储过程输出参数值的更多相关文章

  1. 【C#】【MySQL】C#获取存储过程的Output输出参数值

    创建存储过程 Create PROCEDURE MYSQL @a int, @b int, @c int output AS Set @c = @a + @b GO 通过以下方法可以获得储存过程的输出 ...

  2. ASP.NET MVC + EF 利用存储过程读取大数据,1亿数据测试很OK

    看到本文的标题,相信你会忍不住进来看看! 没错,本文要讲的就是这个重量级的东西,这个不仅仅支持单表查询,更能支持连接查询, 加入一个表10W数据,另一个表也是10万数据,当你用linq建立一个连接查询 ...

  3. ASP.NET MVC + EF 利用存储过程读取大数据

    ASP.NET MVC + EF 利用存储过程读取大数据,1亿数据测试很OK 看到本文的标题,相信你会忍不住进来看看! 没错,本文要讲的就是这个重量级的东西,这个不仅仅支持单表查询,更能支持连接查询, ...

  4. 关于ExecuteNonQuery执行存储过程的返回值 、、实例讲解存储过程的返回值与传出参数、、、C#获取存储过程的 Return返回值和Output输出参数值

    关于ExecuteNonQuery执行存储过程的返回值 用到过ExecuteNonQuery()函数的朋友们在开发的时候肯定这么用过. if(cmd.ExecuteNonQuery("xxx ...

  5. Entity Framework 6 Recipes 2nd Edition(10-10)译 - > 为TPH继承的插入、更新、删除操作映射到存储过程

    10-10. 为TPH继承的插入.更新.删除操作映射到存储过程 问题 TPH继承模型,想把它的插入.修改.删除操作映射到存储过程 Solution 假设数据库有一个描述不同种类的产品表(Product ...

  6. C#获取存储过程返回值和输出参数值的方法

    //转自网络,先留个底 1.获取Return返回值 //存储过程 //Create PROCEDURE MYSQL // @a int, // @b int //AS // return @a + @ ...

  7. asp.net中怎样调用存储过程和存储过程的写法(转载,留着自己看)

    asp.net中怎样调用存储过程和存储过程的写法 创建一个只有输入参数的存储过程 create procedure proc_user@name varchar(20),@Password varch ...

  8. C#获取存储过程的 Return返回值和Output输出参数值

    1.获取Return返回值  程序代码 //存储过程//Create PROCEDURE MYSQL//     @a int,//     @b int//AS//     return @a + ...

  9. mysql时间操作函数和存储过程

    因为业务须要统计一批数据.用到关于mysql的时间操作函数和存储过程,问题已经基本解决.把过程记录下: 1. mysql的语句中不支持直接用循环.循环仅仅能在存储过程中使用. 2. 写为文件时,注意一 ...

随机推荐

  1. elasticsearch从入门到出门-01windows上安装使用

    elasticsearch 1.安装JDK,至少1.8.0_73以上版本,java -version2.下载和解压缩Elasticsearch安装包,目录结构3.启动Elasticsearch:bin ...

  2. Java 重写 equals 与 hashCode 的注意事项

    为什么重写 equals 的时候必须重写 hashCode 大家可能从很多教程中了解到: SUN官方的文档中规定"如果重定义equals方法,就必须重定义hashCode方法,以便用户可以将 ...

  3. [note]树链剖分

    树链剖分https://www.luogu.org/problemnew/show/P3384 概念 树链剖分,是一种将树剖分成多条不相交的链的算法,并通过其他的数据结构来维护这些链上的信息. 最简单 ...

  4. lzugis—搭建属于自己的小型的版本号控制SVN

    版权声明:本文为LZUGIS原创文章,未经同意不得转载. https://blog.csdn.net/GISShiXiSheng/article/details/28643575 对于不了解SVN的同 ...

  5. Ubuntu 13.04 可以使用的源

    以下为收集的Ubuntu 13.04 可以使用的源 #中科大源deb http://mirrors.ustc.edu.cn/ubuntu/ saucy main restricted universe ...

  6. 3.26课·········window.document对象

    1.Window.document对象 一.找到元素:    docunment.getElementById("id"):根据id找,最多找一个:    var a =docun ...

  7. Python 列表解析list comprehension和生成表达式generator expression

    如果想通过操作和处理一个序列(或其他的可迭代对象)来创建一个新的列表时可以使用列表解析(List comprehensions)和生成表达式(generator expression) (1)list ...

  8. 《程序员代码面试指南》第五章 字符串问题 去掉字符串中连续出现k 个0 的子串

    题目 去掉字符串中连续出现k 个0 的子串 java代码 package com.lizhouwei.chapter5; /** * @Description: 去掉字符串中连续出现k 个0 的子串 ...

  9. 总结:iview(基于vue.js的开源ui组件)学习的一些坑

    1.要改变组件的样式 找到这个组件的class名,然后覆盖样式. 举例:修改select框,显示圆角.只需给找到类名并写样 .ivu-select-selection{ border-radius:1 ...

  10. java入门了解12

    1.SequenceInputStream序列流:能将其他输入流的串联 用处:读完第一个再去读第二个输入流 用法:构造方法:SequenceInputStream(InputStream s1,Inp ...