当前位置: 首页 > news >正文

asp.net C#操作存储过程读取存储过程输出参数值

   这段时间在做一个价格平台的项目时候,同事让我写一个存储过程。该存储过程是根据查询条件得出一组新数据,并且返回该组数据的总条数,此处的存储过程我用到了分页,其中主要知识点和难点是之前做项目的时候没有用到过存储过程输出参数,更没有在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[0].Value = title;
                    para[1].Value = pageindex;
                    para[2].Value = pagesize;
                    para[3].Direction = ParameterDirection.Output; //设定参数的输出方向  

                    cmd.Parameters.AddRange(para);// 将参数加入命令对象  
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    DataTable dt = new DataTable();
                    adapter.Fill(dt);

                    resultcount = Convert.ToInt32(cmd.Parameters[3].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
View Code

 

转载于:https://www.cnblogs.com/yabisi/p/5982185.html

相关文章:

  • sync_binlog innodb_flush_log_at_trx_commit 浅析
  • 昂靠的由来[本博作者爆料]
  • js 中 空值赋值运算符 的用法
  • node学习系列之简单文件上传
  • 前端 关于汇率的计算
  • mongoDB 文档查询
  • 安装了python报错 或者执行 npm install 时报node-sass的各种相关错误 解决办法
  • 今天打开一个网站 FSO对象实例创建失败
  • 【LeetCode】5. Longest Palindromic Substring 最大回文子串
  • vu2响应式原理 代码分析
  • 希尔排序
  • vu3响应式原理 代码分析
  • Java Tomcat SSL 服务端/客户端双向认证(一)
  • vue3中 setup注意点
  • redis简介
  • [rust! #004] [译] Rust 的内置 Traits, 使用场景, 方式, 和原因
  • 【跃迁之路】【699天】程序员高效学习方法论探索系列(实验阶段456-2019.1.19)...
  • canvas 绘制双线技巧
  • ES6, React, Redux, Webpack写的一个爬 GitHub 的网页
  • JavaScript设计模式之工厂模式
  • Mybatis初体验
  • python 学习笔记 - Queue Pipes,进程间通讯
  • Python十分钟制作属于你自己的个性logo
  • vue 个人积累(使用工具,组件)
  • vue自定义指令实现v-tap插件
  • 分享几个不错的工具
  • 区块链技术特点之去中心化特性
  • 如何邀请好友注册您的网站(模拟百度网盘)
  • 一文看透浏览器架构
  • 硬币翻转问题,区间操作
  • 用mpvue开发微信小程序
  • 《天龙八部3D》Unity技术方案揭秘
  • ionic入门之数据绑定显示-1
  • TPG领衔财团投资轻奢珠宝品牌APM Monaco
  • (C语言)球球大作战
  • (javascript)再说document.body.scrollTop的使用问题
  • (附源码)php新闻发布平台 毕业设计 141646
  • (生成器)yield与(迭代器)generator
  • (五)关系数据库标准语言SQL
  • (转)linux 命令大全
  • ./configure,make,make install的作用(转)
  • ./indexer: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object fil
  • .NET Core跨平台微服务学习资源
  • .NET/C# 使用反射注册事件
  • .NET处理HTTP请求
  • .NET分布式缓存Memcached从入门到实战
  • .NET高级面试指南专题十一【 设计模式介绍,为什么要用设计模式】
  • .net利用SQLBulkCopy进行数据库之间的大批量数据传递
  • .NET设计模式(2):单件模式(Singleton Pattern)
  • .NET使用HttpClient以multipart/form-data形式post上传文件及其相关参数
  • @data注解_一枚 架构师 也不会用的Lombok注解,相见恨晚
  • [1204 寻找子串位置] 解题报告
  • [2016.7 Day.4] T1 游戏 [正解:二分图 偏解:奇葩贪心+模拟?(不知如何称呼不过居然比std还快)]
  • [Android 数据通信] android cmwap接入点
  • [HNOI2015]实验比较