C# 从C中的存储过程中获取select的值#
我在存储过程中进行选择C# 从C中的存储过程中获取select的值#,c#,tsql,C#,Tsql,我在存储过程中进行选择 SELECT id FROM table WHERE isbn = @Isbn 我想在Web服务中获取此值 因此,我提出: SqlCommand cmd = new SqlCommand("MyStoredProcedure", connection); cmd.CommandType = CommandType.StoredProcedure; object returnObj = cmd.ExecuteScalar(); int returnV
SELECT id
FROM table
WHERE isbn = @Isbn
我想在Web服务中获取此值
因此,我提出:
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总是空的
如何从存储过程中选择值?尝试以下操作:
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);
}
这回答了你的问题吗?您正在传递@Isbn参数吗?