Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ASP.net调用SQL Server存储过程问题。_Asp.net - Fatal编程技术网

ASP.net调用SQL Server存储过程问题。

ASP.net调用SQL Server存储过程问题。,asp.net,Asp.net,我试图调用ASP.NET页面中的存储过程并获取其返回值。这是我的密码: //Something MyConnection.Open(); SqlDataReader reader = MyCommand.ExecuteReader(); if (reader.Read()) { if (returnParameter != null) result = Convert.ToInt32(reader[returnParameter.ParameterName]); } /

我试图调用ASP.NET页面中的存储过程并获取其返回值。这是我的密码:

//Something
MyConnection.Open();
SqlDataReader reader = MyCommand.ExecuteReader();

if (reader.Read())
{
    if (returnParameter != null)
        result = Convert.ToInt32(reader[returnParameter.ParameterName]);
}
//Something else
一切正常,只是
reader.Read()
总是返回false,所以我无法获取返回值。然后执行存储过程。这是从服务器端确认的。没有例外


有什么问题

您必须通读结果集中的最后一条记录,而不仅仅是第一条记录,才能获得返回值。

您使用的是返回值(使用
return
语句)还是输出参数?如果是,则
ExecuteReader
是错误的选择。仅当SP使用
SELECT
语句输出数据时,ExecuteReader才会返回数据。您应该使用带有返回参数的
ExecuteNonQuery
。例如(引用自MSDN)

代码是

// Assumes connection is a valid SqlConnection.
SqlCommand command = new SqlCommand("InsertCategory" , connection);
command.CommandType = CommandType.StoredProcedure;

SqlParameter parameter = command.Parameters.Add(
  "@RowCount", SqlDbType.Int);
parameter.Direction = ParameterDirection.ReturnValue;

parameter = command.Parameters.Add(
  "@CategoryName", SqlDbType.NChar, 15);

parameter = command.Parameters.Add("@Identity", SqlDbType.Int);
parameter.Direction = ParameterDirection.Output;

command.Parameters["@CategoryName"].Value = "New Category";
command.ExecuteNonQuery();

Int32 categoryID = (Int32) command.Parameters["@Identity"].Value;
Int32 rowCount = (Int32) command.Parameters["@RowCount"].Value;

请参见

谢谢您的回复。但我的问题是reader.Read()总是错误的,即使是第一次。它是存储过程-存储在SQL Server中的过程(与存储无关)您能告诉我们存储过程的定义吗?您传入了哪些参数,希望返回哪些参数?谢谢。我算出了答案,答案完全一样。
// Assumes connection is a valid SqlConnection.
SqlCommand command = new SqlCommand("InsertCategory" , connection);
command.CommandType = CommandType.StoredProcedure;

SqlParameter parameter = command.Parameters.Add(
  "@RowCount", SqlDbType.Int);
parameter.Direction = ParameterDirection.ReturnValue;

parameter = command.Parameters.Add(
  "@CategoryName", SqlDbType.NChar, 15);

parameter = command.Parameters.Add("@Identity", SqlDbType.Int);
parameter.Direction = ParameterDirection.Output;

command.Parameters["@CategoryName"].Value = "New Category";
command.ExecuteNonQuery();

Int32 categoryID = (Int32) command.Parameters["@Identity"].Value;
Int32 rowCount = (Int32) command.Parameters["@RowCount"].Value;