asp.net调用接收存储过程返回值

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class _Default : System.Web.UI.Page
{
    //数据库连接字符串
    private string sqlCon = ConfigurationManager.ConnectionStrings["conStr"].ToString();

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    /// 
    /// 获取存储过程return的返回值
    /// 
    /// 
    /// 
    protected void btn_return_Click(object sender, EventArgs e)
    {
        using (SqlConnection con = new SqlConnection(sqlCon))
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            try
            {
                SqlCommand cmd = new SqlCommand("pro_test3", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@id", "1");
                cmd.Parameters.AddWithValue("@ipcount", "100");
                cmd.Parameters.Add("@return", SqlDbType.Int);
                cmd.Parameters["@return"].Direction = ParameterDirection.ReturnValue;
                cmd.ExecuteNonQuery();
                lb_return.Text = cmd.Parameters["@return"].Value.ToString();
            }
            catch (Exception ex)
            {
                //出错信息显示
                lb_return.Text = ex.ToString();
            }
        }
        //对应的存储过程
        //create procedure pro_test1(
        //@id int,@ipcount int)
        //as
        //SET NOCOUNT ON
        //update LogTB set IPCount=@ipcount where Id=@id
        //if @@error <> 0
        //    return -1 
        //else
        //    return 1
        //go
    }

    /// 
    /// 获取存储过程output的返回值
    /// 
    /// 
    /// 
    protected void btn_output_Click(object sender, EventArgs e)
    {
        using (SqlConnection con = new SqlConnection(sqlCon))
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            try
            {
                SqlCommand cmd = new SqlCommand("pro_test3", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@id", "1");
                cmd.Parameters.AddWithValue("@ipcount", "100");
                cmd.Parameters.AddWithValue("@flag", "0");
                cmd.Parameters["@flag"].Direction = ParameterDirection.Output;
                cmd.ExecuteNonQuery();
                lb_return.Text = cmd.Parameters["@flag"].Value.ToString();
            }
            catch (Exception ex)
            {
                //出错信息显示
                lb_return.Text = ex.ToString();
            }
        }
        //对应的存储过程
        //create procedure pro_test2(
        //@id int,@ipcount int,@flag int output)
        //as
        //SET NOCOUNT ON
        //update LogTB set IPCount=@ipcount where Id=@id
        //if @@error <> 0
        //    set @flag = -1 
        //else
        //    set @flag = 1
        //go
    }

    /// 
    /// 获取存储过程返回的数据集
    /// 
    /// 
    /// 
    protected void btn_dataset_Click(object sender, EventArgs e)
    {
        using (SqlConnection con = new SqlConnection(sqlCon))
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            try
            {
                SqlCommand cmd = new SqlCommand("pro_test3", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@id","1");
                using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                {
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
            }
            catch (Exception ex)
            {
                //出错信息显示
                lb_return.Text = ex.ToString();
            }
        }
        //对应的存储过程
        //create procedure pro_test3(
        //@id int)
        //as
        //SET NOCOUNT ON
        //select Id,IPCount from LogTB where Id=@id
        //go
    }
}

你可能感兴趣的:(ASP.NET,asp.net,存储过程,数据库)