308621 PRB:在 Visual C# .NET 中运行 ADO.NET 命令时未返回输出参数 (From MKBA)

本文的发布号曾为 CHS308621
有关本文的 Microsoft Visual Basic .NET 版本,请参见 308051

有关本文的 Microsoft Visual C++ .NET 版本,请参见 308624

本文引用下面的 Microsoft .NET 框架类库名称空间:
  • System.Data.SqlClient
  • System.Data.OleDb

症状

运行 ADO.NET 命令时,输出参数未被初始化或返回错误值。

原因

出现此问题可能有两个原因:
  • 使用 DataReader 对象时,输出参数在数据流的末尾返回。
  • 参数的 Direction 属性设置不正确。

解决方案

若要解决此问题,请使用以下方法之一:
  • 使用 DataReader 对象时,您必须将其关闭或读至数据的末尾才能查看输出参数。
  • 确保参数的方向设置为 Output InputOutput(如果该参数在过程中用于发送和接收数据)。
有关如何实施此解决方案的更多信息,请参见"更多信息"部分。

备注:返回值的参数对象必须是 Parameters 集合的第一项。此外,确保参数的数据类型与期待返回值匹配。

状态

这种现象是设计使然。

更多信息

再现现象的步骤

  1. 在 SQL Server 查询分析器中运行以下查询,在 Pubs 数据库中创建名为"myProc"的存储过程。
    CREATE proc MyProc
    @out smallint OUTPUT
    AS
    Select * from Titles
    Select @out = count(*) from titles
    GO
  2. 启动 Visual Studio .NET。
  3. 在 Visual C# .NET 中新建一个 Windows 应用程序。默认情况下将创建 Form1。
  4. 确保您的项目包含一个对 System.Data 名称空间的引用,如果未包含,请添加一个对此名称空间的引用。
  5. 在 Form1 中放置两个命令按钮。将第一个按钮的 Name 属性更改为 btnDirection,将 Text 属性更改为 Direction。将第二个按钮的 Name 属性更改为 btnReader,将 Text 属性更改为 Reader
  6. System System.Data 名称空间使用 using 语句,这样,以后就不需要在代码中限定这些名称空间中的声明了。
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Data.OleDb;
  7. 返回"窗体"视图,然后双击 Direction 添加单击事件处理程序。将下面的代码添加到处理程序:
    	String myConnString =
    		"User ID=sa;password=password;Initial Catalog=pubs;Data Source=(local)";
    	SqlConnection myConnection = new SqlConnection(myConnString);
    	SqlCommand myCommand = new SqlCommand();
    
    	myCommand.CommandType = CommandType.StoredProcedure;
    	myCommand.Connection = myConnection;
    	myCommand.CommandText = "MyProc";
    	myCommand.Parameters.Add("@out", OleDbType.Integer);
    	//Uncomment this line to return the proper output value.
    	//myCommand.Parameters["@out"].Direction = ParameterDirection.Output;
    	try
    	{
    		myConnection.Open();
    		myCommand.ExecuteNonQuery();
    		MessageBox.Show("Return Value :" + myCommand.Parameters["@out"].Value);
    	}
    	catch (Exception ex)
    	{
    		MessageBox.Show(ex.ToString());
    	}
    	finally
    	{
    		myConnection.Close();
    	}
  8. 返回"窗体"视图,然后双击 Reader 添加单击事件处理程序。将下面的代码添加到处理程序:
    	String myConnString =
    		"User ID=sa;password=password;Initial Catalog=pubs;Data Source=(local)";
    	SqlConnection myConnection = new SqlConnection(myConnString);
    	SqlCommand myCommand = new SqlCommand();
    	SqlDataReader myReader ;
    
    	myCommand.CommandType = CommandType.StoredProcedure;
    	myCommand.Connection = myConnection;
    	myCommand.CommandText = "MyProc";
    	myCommand.Parameters.Add("@out", OleDbType.Integer);
    	myCommand.Parameters["@out"].Direction = ParameterDirection.Output;
    	try
    	{
    		myConnection.Open();
    
    		myReader = myCommand.ExecuteReader();
    
    		//Uncomment this line to return the proper output value.
    		//myReader.Close();
    		MessageBox.Show("Return Value :" + myCommand.Parameters["@out"].Value);
    	}
    	catch (Exception ex)
    	{
    		MessageBox.Show(ex.ToString());
    	}
    	finally
    	{
    		myConnection.Close();
    	} 
  9. 根据您的环境在上述各段示例代码中相应地修改连接字符串 (myConnString)。
  10. 保存项目。
  11. 调试菜单上,单击启动,运行您的项目。
  12. 单击 Direction。注意,返回的是输出参数的错误值。
  13. 若要解决此问题,请取消对设置输出参数 Direction 属性的代码行的注释。运行项目,然后单击 Direction。注意,现在正确返回输出参数。
  14. 单击 Reader。注意,返回的是输出参数的错误值。
  15. 若要解决此问题,取消对关闭 Reader 对象的代码行的注释。运行项目,然后单击 Reader。注意,现在正确返回输出参数。

参考

有关其他信息,请单击下列文章编号,查看 Microsoft 知识库中的文章:

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

这篇文章中的信息适用于:

  • Microsoft ADO.NET(随 .NET 框架一起提供)
  • Microsoft Visual C# .NET (2002)
最近更新:2002-6-19 (1.0)
关键字kbDSupport kbGrpDSMDAC kbGrpDSVBDB kbOLEDB kbprb kbSqlClient kbStoredProc kbSystemData KB308621
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值