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. 存储过程只在创造时进行编译即可,以 ...
随机推荐
- POJ 1258 Agri-Net(最小生成树,基础)
题目 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> #include<math ...
- IT 基础设施
http://www.cnblogs.com/wintersun/p/4355267.html
- VisualSvn Server介绍
1 .VisualSvn Server VisualSvn Server是免费的,而VisualSvn是收费的.VisualSvn是Svn的客户端,和Visual Studio集成在一起,但是不免费 ...
- hdu 1333 Smith Numbers
刚开始没看清题意,要找的数一定要是素数 ;}
- http://blog.csdn.net/superhosts/article/details/15813247
http://blog.csdn.net/superhosts/article/details/15813247
- *[codility]CartesianSequence
https://codility.com/programmers/challenges/upsilon2012 求笛卡尔树的高度,可以用单调栈来做. 维持一个单调递减的栈,每次进栈的时候记录下它之后有 ...
- chm文件访问提示:已取消到该网页的导航
chm文件访问提示:已取消到该网页的导航或不能链接网页 解决方案: 右击chm文件,选择:属性->解除锁定 ,再重新打开文件即可
- SELinux开启与关闭
SELinux是「Security-Enhanced Linux」的简称,是美国国家安全局「NSA=The National Security Agency」 和SCC(Secure Computin ...
- HDU 4351 Digital root 线段树区间合并
依然不是十分理解……待考虑…… #include <cstdio> #include <cstring> #include <cstdlib> #include & ...
- Linux下jvm、tomcat、mysql、log4j优化配置
小菜一直对操作系统心存畏惧,以前也很少接触,这次创业购买了Linux云主机,由于木有人帮忙,只能自己动手优化服务器了.... 小菜的云主机配置大致为:centeos6(32位),4核心cpu,4G内存 ...
