http://support.microsoft.com/kb/310071
HOW TO:使用 ADO.NET 和 Visual C++ .NET 调用带参数的存储过程
此示例使用 ExecuteNonQuery 方法运行查询并返回参数值。ExecuteNonQuery 还返回在运行此查询后受影响的记录数。但是,ExecuteNonQuery 不从该存储过程返回任何行或列。
如果只需要知道更改的行数,那么在使用 INSERT、UPDATE 或 DELETE 语句时,ExecuteNonQuery 方法就特别有用。在存储过程中仅使用 SELECT 语句时,您将收到 -1,因为查询不会影响任何行。
- 在运行 SQL Server 的计算机上创建下面的存储过程:
Create Procedure TestProcedure3 ( @au_idIN varchar (11), @au_fnam varchar (30) ) As Update authors set au_fname = @au_fnam where au_id = @au_idin return (5)
- 启动 Visual Studio .NET,然后在 Visual C++ .NET 中新建一个托管 C++ 应用程序项目。
- 在解决方案资源管理器中,双击源文件(.cpp 文件)。
- 在此源文件中,用下面的代码替换默认代码: SQL 客户机
#include "stdafx.h" #using <mscorlib.dll> #include <tchar.h> #using <system.dll> using namespace System; #using <system.data.dll> using namespace System::Data; using namespace System::Data::SqlClient; // This is the entry point for this application. int _tmain(void) { try{ SqlConnection *myCon = new SqlConnection("Data Source=mySQLServer;User ID=sa; Password=;initial catalog=pubs;"); myCon->Open(); SqlCommand *myCmd = new SqlCommand("TestProcedure3", myCon); myCmd->CommandType = CommandType::StoredProcedure; SqlParameter* retParam=myCmd->Parameters->Add("RetVal",SqlDbType::Int,4); retParam->Direction=ParameterDirection::ReturnValue; myCmd->Parameters->Add("@au_idIN",SqlDbType::VarChar,11); myCmd->Parameters->Item[1]->Value=S"213-46-8915"; myCmd->Parameters->Add("@au_fnam",SqlDbType::VarChar,30); myCmd->Parameters->Item[2]->Value=S"Marjorie"; int cnt; cnt=myCmd->ExecuteNonQuery(); Console::WriteLine("Number of rows affected:{0}; Return Value:{1}", cnt.ToString(),retParam->Value); myCon->Close(); } catch(SqlException *mySqlEx) { for(int i=0;i<mySqlEx->Errors->Count;i++) { Console::WriteLine("Source={0};Message={1};",mySqlEx->Errors-> Item[i]->Source,mySqlEx->Errors->Item[i]->Message); } } catch (System::Exception* ex) { Console::WriteLine(ex->get_Message()); } }
#include "stdafx.h" #using <mscorlib.dll> #include <tchar.h> #using <system.dll> using namespace System; #using <system.data.dll> using namespace System::Data; using namespace System::Data::OleDb; // This is the entry point for this application. int _tmain(void) { try{ OleDbConnection *myCon = new OleDbConnection("Provider=SQLOLEDB.1; Data Source=mySQLServer;User ID=sa; Password=;initial catalog=pubs;"); myCon->Open(); OleDbCommand *myCmd = new OleDbCommand("{?=call TestProcedure3(?,?)}", myCon); myCmd->CommandType = CommandType::Text; OleDbParameter *retParam=myCmd->Parameters->Add("RetVal",OleDbType::Integer,4); retParam->Direction=ParameterDirection::ReturnValue; myCmd->Parameters->Add("au_idIN",OleDbType::VarChar,11); myCmd->Parameters->Item[1]->Value=S"213-46-8915"; myCmd->Parameters->Add("au_fnam",OleDbType::VarChar,30); myCmd->Parameters->Item[2]->Value=S"Marjorie"; int cnt; cnt=myCmd->ExecuteNonQuery(); Console::WriteLine("Number of rows affected:{0}; Return Value:{1}", cnt.ToString(),retParam->Value); myCon->Close(); } catch(OleDbException *mySqlEx) { for(int i=0;i<mySqlEx->Errors->Count;i++) { Console::WriteLine("Source={0};Message={1};",mySqlEx->Errors-> Item[i]->Source,mySqlEx->Errors->Item[i]->Message); } } catch (System::Exception* ex) { Console::WriteLine(ex->get_Message()); } }
- 修改 Connection 对象的连接字符串,以便指向运行 SQL Server 的计算机。
- 按 CTRL+F5 键编译并运行该项目。受影响的行数 (intRowAffected) 和返回参数的值出现在输出窗口中。