简体   繁体   English

使用结果集和返回值调用 SQL 服务器存储过程

[英]Calling SQL Server stored procedure with result set and return value

Is there a way to get a dataset and a return value from a SQL Server stored procedure with just one execution of the stored procedure?有没有办法只执行一次存储过程就可以从 SQL 服务器存储过程中获取数据集和返回值?

Is there a way to get both in just one call?有没有办法在一个电话中同时获得两者?

TIA Marcos Galvani TIA 马科斯伽瓦尼

SqlConnection conn = new SqlConnection(ConnectionString);

// Pick the stored procedure to be executed
SqlCommand cmd = new SqlCommand("CustomersList02", conn);
cmd.CommandType = CommandType.StoredProcedure;

// Set the parameters and return value
cmd.Parameters.Add(new SqlParameter("@Email", email));
cmd.Parameters.Add(new SqlParameter("@ReturnVal", SqlDbType.Int)).Direction = ParameterDirection.ReturnValue;

// Open the connection
conn.Open();

If I do this:如果我这样做:

var dt = new DataTable();
dt.Load(cmd.ExecuteReader());

I don't get the return value, but if I do this:我没有得到返回值,但是如果我这样做:

cmd.ExecuteNonQuery();

I don't get the result set.我没有得到结果集。

You can do this in a single call.您可以在一个电话中完成此操作。

using (SqlDataReader reader = cmd.ExecuteReader())
{
    var dt = new DataTable();
    dt.Load(reader);
}

// getthe return value.
int returnValue = (int)cmd.Parameters["@ReturnVal"].Value;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM