【问题标题】:Get a value of a select from a stored procedures in C#从 C# 中的存储过程中获取选择的值
【发布时间】:2021-07-23 19:00:24
【问题描述】:

我在存储过程中进行选择

SELECT id
FROM table
WHERE isbn = @Isbn

我想在网络服务中获取这个值。

所以我做了这个:

SqlCommand cmd = new SqlCommand("MyStoredProcedure", connection);
cmd.CommandType = CommandType.StoredProcedure;

object returnObj = cmd.ExecuteScalar();
int returnValue = -1;
if (returnObj != null)
{

     int.TryParse(returnObj.ToString(), out returnValue);
}

但 returnValue 始终为 null。

如何从存储过程中的 SELECT 中获取值?

【问题讨论】:

标签: c# tsql


【解决方案1】:

试试这个:

private int LoadUsingOpenReader(SqlDataReader dr)
{
   return (int)dr["id"];
}

SqlDataReader dr = null;
SqlCommand cmd = new SqlCommand("MyStoredProcedure", connection);
cmd.CommandType = CommandType.StoredProcedure;

 dr = cmd.ExecuteReader();
 int returnValue = -1;

 if (dr.Read())
 {
     returnValue = LoadUsingOpenReader(dr);
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    • 2010-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-20
    相关资源
    最近更新 更多