[C++] 调用存储过程 →→→→→进入此内容的聊天室

来自 , 2019-12-16, 写在 C++, 查看 159 次.
URL http://www.code666.cn/view/944626ad
  1. using namespace System;
  2. using namespace System::Data;
  3. using namespace System::Data::SqlClient;
  4.  
  5. void main()
  6. {
  7.     SqlConnection myConnection = nullptr;
  8.    
  9.     try
  10.     {
  11.         myConnection = gcnew SqlConnection("Data Source=localhost;Database=UltraMax; UID=sa; PWD=");        
  12.         myConnection->Open();
  13.        
  14.         SqlCommand myCommand = gcnew SqlCommand("GetMusicByGenre",myConnection);
  15.         myCommand->CommandType = CommandType::StoredProcedure;
  16.        
  17.         SqlCommandBuilder::DeriveParameters(myCommand);
  18.  
  19.         for ( int i = 0; i < myCommand->Parameters->Count; i++ )
  20.             Console::WriteLine("Name={0}; Type={1}; Direction={2}",
  21.                 myCommand->Parameters[i]->ParameterName,
  22.                 myCommand->Parameters[i]->DbType,
  23.                 myCommand->Parameters[i]->Direction);
  24.        
  25.         myCommand->Parameters->Clear();
  26.         myCommand->Parameters->Add("@Genre", SqlDbType::Char);
  27.         myCommand->Parameters["@Genre"]->Value = "Classical";
  28.  
  29.         SqlParameter returnValue = gcnew SqlParameter("RETURN_VALUE",SqlDbType::Int);
  30.         returnValue->Direction = ParameterDirection::ReturnValue;
  31.         myCommand->Parameters->Add(returnValue);
  32.        
  33.         myCommand->ExecuteNonQuery();
  34.  
  35.         Console::WriteLine("Count = {0}",myCommand->Parameters["RETURN_VALUE"]->Value);
  36.     }
  37.     catch(Exception e)
  38.     {
  39.         Console::WriteLine(e->Message);
  40.     }
  41.     finally
  42.     {
  43.         myConnection->Close();
  44.     }
  45. }

回复 "调用存储过程"

这儿你可以回复上面这条便签

captcha