整理sqlserver 级联更新和删除 c#调用存储过程返回值
整理一下级联更新和删除 c#调用返回值
use master
go
IF exists(select 1 from sysdatabases where name='temp')
BEGIN
DROP DATABASE temp
END
create database temp
go
use temp
go
--drop table ProductInfo
create table ProductInfo
(
ProductId int primary key ,
ProductName varchar(20),
) create table ProductDetails
(
id int identity(1,1) primary key,
num varchar(100) ,
ProductId int,
foreign key (ProductId) references ProductInfo(ProductId) on delete cascade on update cascade
) insert ProductInfo values (1,'Think')
insert ProductInfo values(2,'TCL')
insert ProductInfo values(3,'HTC') insert ProductDetails values('T420',1)
insert ProductDetails values('Xo1',1)
insert ProductDetails values('TVoo1',2)
insert ProductDetails values('TPhone',2)
insert ProductDetails values('One',3)
insert ProductDetails values('Buffer',3) alter table 表名
add constraint 外键名
foreign key(字段名) references 主表名(字段名)
on delete cascade --删除
on update cascade --更新 --查看现有数据
select * from ProductInfo
select * from ProductDetails --更改
update ProductInfo set ProductId=5 where ProductName='Think'
select * from ProductInfo
select * from ProductDetails --删除
delete from ProductInfo where ProductId=5
select * from ProductInfo
select * from ProductDetails
第一种方法:
C#代码:
protected void btnBack_Click(object sender, EventArgs e)
{
//调用存储过程
stringconStr=ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ToString();
SqlConnection conn = new SqlConnection(conStr);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "MyProc";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection=conn;
conn.Open();
SqlParameter sp = new SqlParameter("@ID", SqlDbType.Int);
sp.Value = int.Parse("3");
cmd.Parameters.Add(sp); //定义输出参数
SqlParameter returnValue = new SqlParameter("@returnValue", SqlDbType.Int);
returnValue.Direction = ParameterDirection.ReturnValue;
cmd.Parameters.Add(returnValue);
cmd.ExecuteNonQuery();
conn.Close(); }
存储过程如下:
create procedure MyProc
(
@ID int
)
as return 1 go
注意,(return)这种方式 只能返加数值类型 第二种方法:
protected void btnBack_Click(object sender, EventArgs e)
{
//调用存储过程
string conStr = System.Configuration.ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ToString();
SqlConnection conn = new SqlConnection(conStr);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "MyProc";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection=conn;
conn.Open();
SqlParameter sp = new SqlParameter("@ID", SqlDbType.Int);
sp.Value = int.Parse("3");
cmd.Parameters.Add(sp); //定义输出参数
sp = new SqlParameter("@outputValue", SqlDbType.NVarChar,50);
sp.Direction = ParameterDirection.Output;
cmd.Parameters.Add(sp);
cmd.ExecuteNonQuery(); conn.Close(); } 存储过程如下:
alter procedure MyProc
(
@ID int,
@outputValue nvarchar(50) output )
as
Select @outputValue='aa'
go
整理sqlserver 级联更新和删除 c#调用存储过程返回值的更多相关文章
- SQL图形化操作设置级联更新和删除
SQL级联操作设置 对SQL数据库的表,进行级联操作(如级联更新及删除),首先需要设置表的主外键关系,有两种方法: 第一种: 1. 选择你要进行操作的数据库 2. 为你要创建关系的两个 ...
- myabatis oracle 调用存储过程返回list结果集
Mapper.xml 配置 <resultMap type="emp" id="empMap"> <id property="emp ...
- shell调用函数返回值深入分析
编写shell脚本过程中,我们经常会自定义一些函数,并根据函数的返回值不同来执行相应的流程,那么我们如何来获取函数的返回值呢? 首先shell中调用函数有两种方式: 第一种:value=`functi ...
- PostgreSQL 调用存储过程返回结果集
创建返回结果集类型的存储过程: CREATE OR REPLACE FUNCTION public.f_get_member_info( id integer, productname charact ...
- ASP.NET调用存储过程并接收存储过程返回值
ASP.NET调用存储过程并接收存储过程返回值 2010-08-02 11:26:17| 分类: C#|字号 订阅 2010年02月27日 星期六 23:52 假设表结构Create T ...
- oracle学习 十二 使用.net程序调用带返回值的存储过程(持续更新)
数据库返回的是结果集,存储过程返回的是一个或者多个值,所以不要使用while循环去读取,也不要使用datareader函数去调用.v_class_name是返回函数 使用.net调用oracle数据库 ...
- sqlserver,获取调用存储过程返回数据的方法。
1,获取存储过程最后select返回的结果集.SELECT 数据集返回值. 因为select返回的结果是一个表.所以返回的结果需要用一个表接收.使用临时表接收. 被调用的存储过程最后是这样:返回了一个 ...
- (转载)SQLServer存储过程返回值总结
1. 存储过程没有返回值的情况(即存储过程语句中没有return之类的语句) 用方法 int count = ExecuteNonQuery(..)执行存储过程其返回值只有两种情况 (1)假如通过查询 ...
- SQLServer存储过程返回值总结
1. 存储过程没有返回值的情况(即存储过程语句中没有return之类的语句) 用方法 int count = ExecuteNonQuery(..)执行存储过程其返回值只有两种情况 (1)假如通 ...
随机推荐
- 阶乘 求n!中质因数的个数
在n!中末尾有几个0 取决于n!中5的个数(2肯定比5多) 所以遍历从1到n的数,看总共有几个5即可 ..N do j = i; == ) ++ret; j /= ; end end 有个nb的方法: ...
- oracle树操作(select start with connect by prior)
oracle中的递归查询可以使用:select .. start with .. connect by .. prior 下面将会讲述oracle中树形查询的常用方式,只涉及到一张表. 一. 建表语句 ...
- 初试cocos2d-x坐标系
bool HelloWorld::init() { ////////////////////////////// if ( !Layer::init() ) { return false; } Siz ...
- Python 部署项目(Tomcat 容器)
此前书写了多实例的 Tomcat 启动等操作的脚本,今天完善 Tomcat 多实例部署(本脚本只提供思路) 脚本内容: #!/usr/bin/env python # _*_coding:utf-8_ ...
- 我的NopCommerce之旅(6): 应用启动
一.基础介绍 Global.asax 文件(也称为 ASP.NET 应用程序文件)是一个可选文件,该文件包含响应 ASP.NET 或 HTTP 模块所引发的应用程序级别和会话级别事件的代码. Appl ...
- 边工作边刷题:70天一遍leetcode: day 81-1
Alien Dictionary 要点:topological sort,bfs 只有前后两个word之间构成联系,一个word里的c是没有关系的 只要得到两个word第一个不同char之间的part ...
- 边工作边刷题:70天一遍leetcode: day 85-3
Zigzag Iterator 要点: 实际不是zigzag而是纵向访问 这题可以扩展到k个list,也可以扩展到只给iterator而不给list.结构上没什么区别,iterator的hasNext ...
- python中list注意事项
今天写python出了一个小插曲,具体情况见代码: >>> a = [1,2,3] >>> import queue >>> q = queue. ...
- Linux搭建python环境中cx_Oracle模块安装遇到的问题与解决方法
安装或使用cx_Oracle时,需要用到Oracel的链接库,如libclntsh.so.11.1,否则会有各种各样的错误信息. 安装Oracle Instant Client就可得到这个链接库,避免 ...
- Watir、Selenium2、QTP区别
1.支持的语言 Watir:ruby Selenium2:支持多种语言,如:python,ruby,java,c#,php,perl,javascript QTP:vbscript 2.支持的浏览器 ...