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. 存储过程只在创造时进行编译即可,以 ...
随机推荐
- hdoj 1102 Constructing Roads
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1102 分析:看到这题给出的都是矩阵形式,就知道了可以用Prim算法求MST. #include <i ...
- java.lang.NoClassDefFoundError: JspException
在打开jsp页面的时候报错java.lang.NoClassDefFoundError: JspException,如下所示: 原因和解决方案: 原因是由于包不全 把该导的包导进去,在上面的例子就是由 ...
- lintcode:最大子数组差
题目 最大子数组差 给定一个整数数组,找出两个不重叠的子数组A和B,使两个子数组和的差的绝对值|SUM(A) - SUM(B)|最大. 返回这个最大的差值. 样例 给出数组[1, 2, -3, 1], ...
- application.xml定时
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- 核心思想:百度网盘怎么盈利(互联网的高速更新决定了:亏钱你还有点机会,放弃连门都进不了),战略预备队 good
百度做网盘很大程度就是为了防止别人依靠网盘做大和积累点技术储备.腾讯邮箱怎么赚钱?腾讯影音怎么赚钱?互联网的高速更新决定了,一些你看不起眼的软件很可能就会席卷整个市场,所以互联网大佬宁愿一些项目亏钱也 ...
- QString转换成LPCWSTR
不知道大家用QT做什么程序,但应该还是Windows程序居多吧,仍免不了与WINAPI打交道.这就要求数据类型要与Windows一致,而QT的数据类型却是自成一体的.这就需要转换.比如我用到了Logo ...
- 无刷新分页 jquery.pagination.js
无刷新分页 jquery.pagination.js 采用Jquery无刷新分页插件jquery.pagination.js实现无刷新分页效果 1.插件参数列表 http://www.dtan.so ...
- 7、SpringMVC源码分析(2):分析HandlerAdapter.handle方法,了解handler方法的调用细节以及@ModelAttribute注解
从上一篇 SpringMVC源码分析(1) 中我们了解到在DispatcherServlet.doDispatch方法中会通过 mv = ha.handle(processedRequest, res ...
- init_MUTEX
2.6.25及以后的linux内核版本废除了init_MUTEX函数 新版本使用sema_init函数 平台:X86 32位内核:2.6.24定义: 引用 static inline void ini ...
- IP地址分类及私网IP
5类IP地址: IP地址共有32位字节,其中A~C类IP地址由类标识号.网络地址和主机地址组成,A类标识最高位为0,网络地址为1字节,主机地址为3字节, B类标识最高位为10,网络地址为2字节,主机地 ...
