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
    

      

  • 相关阅读:
    ViewPagerAdapter
    Android Touch事件传递机制详解
    android ANR产生原因和解决办法【转】
    Android 操作系统的内存回收机制(转载)
    android的程序运行数据存放在哪里?
    自定义RecyclerView.ItemDecoration,实现RecyclerView的分割线效果
    Android中 Bitmap和Drawable相互转换的方法
    android 存储图片到data目录和读取data目录下的图片
    多线程调用HttpWebRequest并发连接限制
    反射
  • 原文地址:https://www.cnblogs.com/li-peng/p/3254982.html
Copyright © 2011-2022 走看看