【數據庫】如何獲取存儲過程的輸出參數值


存儲過程:

-- =============================================
-- 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>代碼后頁面布局變化問題


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM