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. 存储过程只在创造时进行编译即可,以 ...
随机推荐
- Javascript实现两张图片的延迟加载
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- secure CRT 介绍
SecureCRT是一款支持SSH(SSH1和SSH2)的终端仿真程序,简单地说是Windows下登录UNIX或Linux服务器主机的软件. Secure[1] CRT支持SSH,同时 ...
- Oracle的学习一:安装与卸载、sql *plus常用命令、Oracle用户管理
1.为什么学习oracle? 性能优越: 小型数据库 中型数据库 大型数据库 acess.foxbase mysql.sql server.informix sybase.oracle.db2 复杂量 ...
- linux入门教程(五) Linux系统的远程登录
首先要说一下,该部分内容对于linux初学者来讲并不是特别重要的,可以先跳过该章节,先学下一章,等学完后再回来看这一章. Linux大多应用于服务器,而服务器不可能像PC一样放在办公室,它们是放在ID ...
- css中的:before与:after的简单使用
直接上代码,不多说了! 画三角形 1). <style type="text/css"> .triangle{ width: 0; height: 0; /*borde ...
- Google不做坏事吗?
说中国足球为什么冲不出亚洲,那是因为咱中国人太文气,足球是种“斗牛士”式的游戏,得玩的有点儿“野蛮”色彩.记得以前在英国的时候,遇上联赛,晚上大街小巷全民皆兵,曼切斯特队的粉丝在街道一边酒吧里,利物浦 ...
- 数据类型演示DataTypeDemo
/***数据类型演示*/public class DataTypeDemo{ public static void main(String[] args){ //直接赋予的值,称为字面量 //by ...
- POJ3009——Curling 2.0(DFS)
Curling 2.0 DescriptionOn Planet MM-21, after their Olympic games this year, curling is getting popu ...
- 关于Linux的windows目录的挂载
今天,linux主机下面要增加一点空间,不想再增加硬盘,所以就在实体机里面就设置了目录共享,添加自己系统的默认账号(为了增加自己主机的安全性,我都是设置的含有标点符号的密码---这也是这次挂载不成功的 ...
- IOS打包脚本
1.xcodebuild clean -project $projectname.xcodeproj -configuration Release -alltargets2.xcodebuild ar ...
