c#調用帶輸出參數的預存程序

來源:互聯網
上載者:User

標籤:os   io   ar   資料   cti   sp   log   on   c   

CREATE PROCEDURE ProGetPWD  

   @username varchar(20),  

   @password varchar(20) OUTPUT  

AS  

BEGIN  

   SELECT @password = password   

   FROM Users   

   WHERE username = @username  

END  

 

 

string sql = "Data Source=.;Initial Catalog=Test;Integrated Security=True;";
using (SqlConnection conn = new SqlConnection(sql))
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
//設定要調用的預存程序的名稱
cmd.CommandText = "GetuserPwd";
//指定SqlCommand對象傳給資料庫的是預存程序的名稱而不是sql語句
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter userName = cmd.Parameters.Add(new SqlParameter("@userName", SqlDbType.VarChar, 20));
//指明"@userName"是輸入參數
userName.Direction = ParameterDirection.Input;
//為@userName參數賦值
userName.Value = txtUserName.Text;
SqlParameter userPwd = cmd.Parameters.Add(new SqlParameter("@userPwd", SqlDbType.VarChar, 20));
//指明"@userPwd"是輸出參數
userPwd.Direction = ParameterDirection.Output;
//執行
cmd.ExecuteNonQuery();
//得到輸出參數的值,把賦值給name,注意,這裡得到的是object類型的,要進行相應的類型輪換
string passwrod = Convert.ToString(cmd.Parameters["@userPwd"].Value);
MessageBox.Show(passwrod);

}

c#調用帶輸出參數的預存程序

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.