【数据库】如何获取存储过程的输出参数值

存储过程:

-- =============================================
-- Author:        netboy
-- Create date: 2012年7月18日
-- Description:    通过cid得到用户一级密码
-- =============================================
CREATE PROC [dbo].[GETONEPED]
    @C_ID nvarchar(50),
    @onepwd nvarchar(50) output
AS
BEGIN
    select @onepwd = C_OnePwd FROM Client where C_ID=@C_ID
END

功能:根据C_ID查询字段onepwd

asp.net后台代码:

        protected void Button1_Click(object sender, EventArgs e)
        {
            string cid = txbid.Text;
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["huaxia"].ConnectionString))    //连接数据库语句
            {
                using (SqlCommand com = con.CreateCommand())
                {
                    con.Open();
                    com.CommandText = "GETONEPED";      //存储过程
                    com.CommandType = CommandType.StoredProcedure;      //设置执行命令类型

                    com.Parameters.Add("@C_ID", SqlDbType.NVarChar);    //设置输入参数 cid
                    com.Parameters["@C_ID"].Value = cid;                //设置cid参数的值

                    com.Parameters.Add("@onepwd", SqlDbType.NVarChar, 50);      //设置输出参数onepwd
                    com.Parameters["@onepwd"].Direction = ParameterDirection.Output;    //设置参数onepwd为输出类型
                   
                    com.ExecuteNonQuery();          //执行命令

                    string name = com.Parameters["@onepwd"].Value.ToString();  
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('" + name + "'); ", true);

                }
            }
        }

关于最后一句代码详情见:【ASP.NET】解决执行<script>代码后页面布局变化问题

原文地址:https://www.cnblogs.com/ngnetboy/p/2599581.html