第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

在ADO.NET中獲取輸出參數(shù)值

在ADO.NET中獲取輸出參數(shù)值

拉風(fēng)的咖菲貓 2019-07-30 17:03:20
在ADO.NET中獲取輸出參數(shù)值我的存儲(chǔ)過(guò)程有一個(gè)輸出參數(shù):@ID INT OUT如何使用ado.net檢索此內(nèi)容?using (SqlConnection conn = new SqlConnection(...)){     SqlCommand cmd = new SqlCommand("sproc", conn);     cmd.CommandType = CommandType.StoredProcedure;     // add parameters     conn.Open();     // *** read output parameter here, how?     conn.Close();}
查看完整描述

3 回答

?
飲歌長(zhǎng)嘯

TA貢獻(xiàn)1951條經(jīng)驗(yàn) 獲得超3個(gè)贊

其他反應(yīng)表明這一點(diǎn),但實(shí)際上你只需要?jiǎng)?chuàng)建一個(gè)SqlParameter,設(shè)置Direction來(lái)Output,并把它添加到SqlCommandParameters集合。然后執(zhí)行存儲(chǔ)過(guò)程并獲取參數(shù)的值。

使用您的代碼示例:

// SqlConnection and SqlCommand are IDisposable, so stack a couple using()'susing (SqlConnection conn = new SqlConnection(connectionString))using (SqlCommand cmd = new SqlCommand("sproc", conn)){
   // Create parameter with Direction as Output (and correct name and type)
   SqlParameter outputIdParam = new SqlParameter("@ID", SqlDbType.Int)
   { 
      Direction = ParameterDirection.Output 
   };

   cmd.CommandType = CommandType.StoredProcedure;
   cmd.Parameters.Add(outputIdParam);

   conn.Open();
   cmd.ExecuteNonQuery();

   // Some various ways to grab the output depending on how you would like to
   // handle a null value returned from the query (shown in comment for each).

   // Note: You can use either the SqlParameter variable declared
   // above or access it through the Parameters collection by name:
   //   outputIdParam.Value == cmd.Parameters["@ID"].Value

   // Throws FormatException
   int idFromString = int.Parse(outputIdParam.Value.ToString());

   // Throws InvalidCastException
   int idFromCast = (int)outputIdParam.Value; 

   // idAsNullableInt remains null
   int? idAsNullableInt = outputIdParam.Value as int?; 

   // idOrDefaultValue is 0 (or any other value specified to the ?? operator)
   int idOrDefaultValue = outputIdParam.Value as int? ?? default(int); 

   conn.Close();}

在獲取時(shí)要小心Parameters[].Value,因?yàn)樾枰獙㈩愋娃D(zhuǎn)換為object您聲明的類型。而SqlDbType當(dāng)您創(chuàng)建使用SqlParameter需求來(lái)匹配數(shù)據(jù)庫(kù)類型。如果您只是將其輸出到控制臺(tái),您可能只是在使用Parameters["@Param"].Value.ToString()(通過(guò)Console.Write()String.Format()調(diào)用顯式或隱式)。


查看完整回答
反對(duì) 回復(fù) 2019-07-30
  • 3 回答
  • 0 關(guān)注
  • 717 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)