• 调用存储过程的一些方法


    protected void Button1_Click(object sender, EventArgs e)
       {
        
           int number =Convert.ToInt32(TextBox1.Text.ToString().Trim());
           int tid =Convert.ToInt32(TextBox2.Text.ToString().Trim());
           int k = exec_proc_2(number, tid);
           bind();
           Response.Write(k.ToString());
        
         
       }
       protected void bind()
       {
           SqlConnection conn = new SqlConnection("server=(local);database=yiqi;uid=monkey;pwd=donkey");
           conn.Open();
           SqlCommand cmd = new SqlCommand("proc_3", conn);
           cmd.CommandType = CommandType.StoredProcedure;
           DataTable dt = new DataTable();
           using (SqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
           {
               dt.Load(sdr);
           }
           GridView1.DataSource = dt;
           GridView1.DataBind();

       }

       public static int exec_proc_2(int number, int tid)
       {
           SqlConnection conn = new SqlConnection("server=(local);database=yiqi;uid=monkey;pwd=donkey");
           conn.Open();
           SqlCommand cmd = new SqlCommand("proc_2", conn);//同上
           cmd.CommandType = CommandType.StoredProcedure;//同上
           cmd.Parameters.Add(new SqlParameter("@result", SqlDbType.Int));//添加一个名为@result的参数,数据类型为SqlDbType.Int
           cmd.Parameters["@result"].Direction = ParameterDirection.Output;//将@result参数设置成为接收输出参数
           cmd.Parameters.AddWithValue("@number", number);
           cmd.Parameters.AddWithValue("@tid", tid);
           cmd.ExecuteNonQuery();
           int result = (int)cmd.Parameters["@result"].Value;//将输出的Object数据转换成int类型
           return result;
       }

    -----------------------------------------------------------------------------------------------------

    set ANSI_NULLS ON
    set QUOTED_IDENTIFIER ON
    go


    ALTER PROC [dbo].[proc_1]

    AS

    Insert t1(number) Values(200)

    set ANSI_NULLS ON
    set QUOTED_IDENTIFIER ON
    go


    ALTER PROC [dbo].[proc_2]

    @result int output,

    @number int,

    @tid int

    AS

    Update t1 Set number=number+@number Where tid=@tid

    Select @result=number From t1

    set ANSI_NULLS ON
    set QUOTED_IDENTIFIER ON
    go


    ALTER PROC [dbo].[proc_3]
    AS
    select * from t1

    --------------------------------------

    Create Table Table_1

    (

    tid int identity(1,1),

    number int

    )

    天道酬勤,厚积薄发。 君子之行,静以修身,俭以养德。 非淡泊无以明志,非宁静无以致远。 如有恒,何须三更起,半夜眠;最怕莫,三天打鱼两天晒网,竹篮打水一场空。
  • 相关阅读:
    JAVA开源B2C系统
    在IDEA中将SpringBoot项目打包成jar包的方法
    国外的开源项目Shopizer部署问题
    SpringBoot集成RabbitMQ
    隐藏网页中DIV和DOM的各种方法
    SpringCloud之网关 Zuul(四)
    SpringCloud之声明式服务调用 Feign(三)
    SpringCloud之实现客户端的负载均衡Ribbon(二)
    SpringCloud之服务注册与发现Eureka(一)
    IntelliJ IDEA maven springmvc+shiro简单项目
  • 原文地址:https://www.cnblogs.com/houweidong/p/3008669.html
Copyright © 2020-2023  润新知