ADO.NET访问SQL Server调用存储过程带回参
1,ADO.NET访问SQL Server调用存储过程带回参 |
2,DatabaseDesign |
use northwind
go
--存储过程1
--插入一条商品 productname=芹菜 unitprice=2.3
create proc p_insert
@productname varchar(40),
@unitprice money
as
insert products(productname,unitprice)
values(@productname,@unitprice)
go
--执行
exec p_insert '芹菜',2.3 --存储过程2
--查全部商品
create proc p_selectall
as
select * from products
go
--执行
exec p_selectall --存储过程3
--根据商品编号=1,商品名称和单价
create proc p_selectbyid
@productid int, --入参
@productname varchar(40) output,--出参
@unitprice money output --出参
as
select @productname=productname,@unitprice=unitprice from products where productid=@productid --执行
declare @name varchar(40)
declare @price money
exec p_selectbyid @productname=@name output, @unitprice=@price output, @productid=1
select @name,@price
3,Code |
3.1,Program.cs
using System; using System.Data.SqlClient;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{ ////测试存储过程1
// SqlConnection con = new Com.Myt.DB.DBConnection().Con;
// SqlCommand com = con.CreateCommand(); // com.CommandText = "usp_insert";
// com.CommandType = System.Data.CommandType.StoredProcedure;//命令的类型是储存过程
// com.Parameters.Add(new SqlParameter("@productname", "苹果"));
// com.Parameters.Add(new SqlParameter("@unitprice", 200)); // con.Open();
// int count= com.ExecuteNonQuery(); // con.Close();
// Console.WriteLine("一共影响了"+count+"行"); ////存储过程2,测试返回结果集
//SqlConnection con = new Com.Myt.DB.DBConnection().Con;
//SqlCommand com = con.CreateCommand(); //com.CommandText = "usp_selectall";
//com.CommandType = System.Data.CommandType.StoredProcedure; //con.Open();
//SqlDataReader sdr = com.ExecuteReader();
//while (sdr.Read())
//{ // Console.WriteLine(sdr.GetString(0)+"\t"+sdr.GetDecimal(1));
//} //com.Clone(); //存储过程3,测试输出参数
//已知商品编号,查名称和单价
SqlConnection con = new Com.Myt.DB.DBConnection().Con;
SqlCommand com = con.CreateCommand(); com.CommandText = "usp_selectbyid";
com.CommandType = System.Data.CommandType.StoredProcedure; //配参,注意出参的配置
//入参
com.Parameters.Add(new SqlParameter("@productid", )); //出参
SqlParameter p1 = new SqlParameter("@productname", System.Data.SqlDbType.VarChar, );
SqlParameter p2 = new SqlParameter("@unitproduct", System.Data.SqlDbType.Decimal);
//标明输出方向
p1.Direction = System.Data.ParameterDirection.Output;
p2.Direction = System.Data.ParameterDirection.Output;
com.Parameters.Add(p1);
com.Parameters.Add(p2); con.Open(); com.ExecuteNonQuery();
con.Close(); Console.WriteLine(p1.Value+", "+p2.Value);
}
}
}
3.2,DBConnection.cs
4, |
![]() |
作者:ylbtech 出处:http://ylbtech.cnblogs.com/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 |
ADO.NET访问SQL Server调用存储过程带回参的更多相关文章
- ASP调用存储过程访问SQL Server
ASP调用存储过程访问SQL Server 2011-02-15 10:22:57 标签:asp 数据库 sQL 存储过程 Server ASP和存储过程(Stored Procedures)的文章 ...
- 原生的ado.net(访问sql server数据库)
本文介绍原生的ado.net(访问sql server数据库) 写在前面 数据库连接字符串 过时的写法 string str = "server=localhost;database=my_ ...
- 在易语言中调用MS SQL SERVER数据库存储过程方法总结
Microsoft SQL SERVER 数据库存储过程,根据其输入输出数据,笼统的可以分为以下几种情况或其组合:无输入,有一个或多个输入参数,无输出,直接返回(return)一个值,通过output ...
- SQL Server中的CLR编程——用.NET为SQL Server编写存储过程和函数
原文:SQL Server中的CLR编程--用.NET为SQL Server编写存储过程和函数 很早就知道可以用.NET为SQL Server2005及以上版本编写存储过程.触发器和存储过程的,不过之 ...
- sql server系统存储过程大全
关键词:sql server系统存储过程,mssql系统存储过程 xp_cmdshell --*执行DOS各种命令,结果以文本行返回. xp_fixeddrives --*查询各磁盘/分区可用空间 x ...
- 在sql server中建存储过程,如果需要参数是一个可变集合怎么处理?
在sql server中建存储过程,如果需要参数是一个可变集合的处理 原存储过程,@objectIds 为可变参数,比如 110,98,99 ALTER PROC [dbo].[Proc_totalS ...
- SQL Server中存储过程 比 直接运行SQL语句慢的原因
问题是存储过程的Parameter sniffing 在很多的资料中都描述说SQLSERVER的存储过程较普通的SQL语句有以下优点: 1. 存储过程只在创造时进行编译即可,以后每次执行存储过 ...
- JDBC连接SQL server与ADO.NET连接Sql Server对比
JDBC连接SQL server与ADO.NET连接Sql Server对比 1.JDBC连接SQL server 1)java方面目前有很多驱动能够驱动连接SQL servernet. 主流的有 ...
- SQL Server中存储过程比直接运行SQL语句慢的原因
原文:SQL Server中存储过程比直接运行SQL语句慢的原因 在很多的资料中都描述说SQLSERVER的存储过程较普通的SQL语句有以下优点: 1. 存储过程只在创造时进行编译即可,以 ...
随机推荐
- UVA 10246 Asterix and Obelix
题意:每个城市举办庆祝有一定的花费,A在路径上会选择庆祝花费最大的城市 让你求,A回家所花的路费和庆祝费最少,也就是说并不是最短路径就是结果, 还有可能就是路费比最短路径的多,但是庆祝费就比它的少,总 ...
- POJ 1988 Cube Stacking(带权并查集)
哈哈,一次AC. 题意:给你 1-n 编号的立方体,然后移动包含指定编号的立方体的堆移到另一个堆上边, 询问指定的编号立方体下面有多少个立方体. 思路:由于并查集是存储的是它的父亲,那么只能从父亲那里 ...
- Tomcat启用Gzip压缩
原理简介 HTTP 压缩可以大大提高浏览网站的速度,它的原理是,在客户端请求服务器对应资源后,从服务器端将资源文件压缩,再输出到客户端,由客户端的浏览器负责解压缩并 浏览.相对于普通的 ...
- form表单中的enctype属性什么意思?
enctype就是encodetype翻译成中文就是编码类型的意思!multipart/form-data是指表单数据有多部分构成:既有文本数据,又有文件等二进制数据的意思.另外需要注意的是:默认情况 ...
- Android中JSON数据格式的简单使用
源码: package com.wangzhu.demo; import java.io.BufferedReader; import java.io.IOException; import java ...
- PCB工艺镀金(电金)和沉金(化金)的区别
1.镀金和沉金的别名分别是什么? 镀金:硬金,电金(镀金也就是电金) 沉金:软金,化金 (沉金也就是化金) 2.别名的由来: 镀金:通过电镀的方式,使金粒子附着到pcb板上,所以叫电金,因为附着 ...
- Xaml语法概述及属性介绍
Xaml语法概述 1.命名空间 xmal每个元素都对应着一个类,但是在xmal中,只提供类名是不够的,需要知道该类实在.net的哪个命名空间下面.Xaml解析器才能够正确的解析. 1 < ...
- PHP裁剪图片
PHP裁剪图片 $src_path = '1.jpg'; //创建源图的实例 $src = imagecreatefromstring(file_get_contents($src_path)); / ...
- HDU 4604 deque 最长上升子序列
枚举每个位置,求以num[i]为起点的最长不下降子序列和以num[i]为结尾的最长不递增子序列. 并且把相同值的个数统计一下,最后要减去算重复了的. 比如: 1 9 4 4 2 2 2 3 3 3 7 ...
- PostgreSql中如何kill掉正在执行的sql语句
虽然可以使用 kill -9 来强制删除用户进程,但是不建议这么去做. 因为:对于执行update的语句来说,kill掉进程,可能会导致Postgres进入到recovery mode 而在recov ...