HOW TO:使用 ADO.NET 和 Visual C++ .NET 调用带参数的存储过程

http://support.microsoft.com/kb/310071

 

HOW TO:使用 ADO.NET 和 Visual C++ .NET 调用带参数的存储过程

此示例使用 ExecuteNonQuery 方法运行查询并返回参数值。ExecuteNonQuery 还返回在运行此查询后受影响的记录数。但是,ExecuteNonQuery 不从该存储过程返回任何行或列。

如果只需要知道更改的行数,那么在使用 INSERT、UPDATE 或 DELETE 语句时,ExecuteNonQuery 方法就特别有用。在存储过程中仅使用 SELECT 语句时,您将收到 -1,因为查询不会影响任何行。

  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)
  2. 启动 Visual Studio .NET,然后在 Visual C++ .NET 中新建一个托管 C++ 应用程序项目。
  3. 在解决方案资源管理器中,双击源文件(.cpp 文件)。
  4. 在此源文件中,用下面的代码替换默认代码: 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());
        }
    }
    OLE DB 数据提供程序
    #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());
        }
    
    }	 
  5. 修改 Connection 对象的连接字符串,以便指向运行 SQL Server 的计算机。
  6. 按 CTRL+F5 键编译并运行该项目。受影响的行数 (intRowAffected) 和返回参数的值出现在输出窗口中。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值