zoukankan      html  css  js  c++  java
  • 整理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
    

      

  • 相关阅读:
    Dynamics AX 2012 R2 配置E-Mail模板
    Dynamics AX 2012 R2 设置E-Mail
    Dynamics AX 2012 R2 为运行失败的批处理任务设置预警
    Dynamics AX 2012 R2 耗尽用户
    Dynamics AX 2012 R2 创建一个专用的批处理服务器
    Dynamics AX 2012 R2 创建一个带有负载均衡的服务器集群
    Dynamics AX 2012 R2 安装额外的AOS
    Dynamics AX 2012 R2 将系统用户账号连接到工作人员记录
    Dynamics AX 2012 R2 从代码中调用SSRS Report
    Dynamics AX 2012 R2 IIS WebSite Unauthorized 401
  • 原文地址:https://www.cnblogs.com/li-peng/p/3254982.html
Copyright © 2011-2022 走看看