整理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)假如通 ...
随机推荐
- .Net 三款工作流引擎比较:WWF、netBPM 和 ccflow
下面将对目前比较主流的三款工作流进行介绍和比较,然后通过三款流程引擎分别设计一个较典型的流程来给大家分别演示这三款创建流程的过程.这三款工作流程引擎分别是 Windows Workflow Found ...
- Effective Java 20 Prefer class hierarchies to tagged classes
Disadvantage of tagged classes 1. Verbose (each instance has unnecessary irrelevant fields). 2. Erro ...
- MySQL 之 query cache
早上一打开网站,就看到了Percona官网发布的最新的关于 mysql query cache的文章: https://www.percona.com/blog/2015/08/07/mysql-qu ...
- Spring中Template模式与callback的结合使用浅析
Spring不论是与ibatis,还是与Hibernate的结合中,都使用到了Template模式与callback技术,来达到简化代码实现的目的.Template模式也即模板模式,用于对一些不太变化 ...
- 实验:sigsuspend(),sigprocmask()
实验:sigsuspend(),sigprocmask() 源代码: /* * Program: pause_suspend.c * To test the difference between si ...
- SSIS XML source demo
以下是一个使用xml作为source的SSIS package示例: 自动生成的xsd.把两个结点merge join成一条记录. 示例XML如下: <?xml version="1. ...
- zookeeper适用场景:分布式锁实现
问题导读:1.zookeeper如何实现分布式锁?2.什么是羊群效应?3.zookeeper如何释放锁? 在zookeeper应用场景有关于分布式集群配置文件同步问题的描述,设想一下如果有100台机器 ...
- Virtual Box上安装CentOS7
问题1:安装完没有桌面系统(Gnome或KDE)解答:安装的时候,软件选择为最小安装",更改该选择 问题2:开机提示Initial setup of CentOS Linux 7 (core ...
- Linux (二) vi
1 步骤 1) vi test.txt 进入一般模式 2) i 进入编辑模式,输入内容 3) Esc 回到一般模式 4) :wq 存储后退出 2 编辑模式 [i] 光标处插入, ...
- 合工大 OJ 1332 蛇形阵
Description 蛇形针回字阵: 如3*3: 回字阵: 7 6 5 8 1 4 9 2 3 Input 多组数据: 每一行一个正整数n(n为奇数,<26),代表n*n矩阵. Output ...