6
我试图获得SQL 2008服务器上的存储过程的结果集和返回值。当我在sql管理工作室运行proc时,我得到了结果集和返回值。返回值使用SqlCommand
但是,当我尝试获取C#4.0中的值时,该参数的值为null。这是我的C#代码:
using (ConnectionManager<SqlConnection> cn = ConnectionManager<SqlConnection>.GetManager(CultureInfo.CurrentCulture.Name))
{
using (SqlCommand cm = cn.Connection.CreateCommand())
{
cm.CommandText = "Name of proc here";
cm.CommandType = CommandType.StoredProcedure;
cm.Parameters.AddWithValue("@ApplicationId", ApplicationId);
cm.Parameters.AddWithValue("@Index", Index);
if (PageSize > 0)
cm.Parameters.AddWithValue("@PageSize", PageSize);
cm.Parameters.Add("@ReturnValue", SqlDbType.Int);
cm.Parameters["@ReturnValue"].Direction = ParameterDirection.ReturnValue;
using (IDataReader dr = cm.ExecuteReader())
{
SafeDataReader sdr = new SafeDataReader(dr);
while (sdr.Read())
{
UserApplicationEntity uae = new UserApplicationEntity();
uae.UserId = sdr.GetGuid("UserId");
uae.ExternalId = sdr.GetString("ExternalId");
Result.Value.Collection.Add(uae);
}
Result.Value.TotalResults = (int)cm.Parameters["@ReturnValue"].Value;
}
}
}
最后一行,其中我请Result.Value.TotalResults =(int)的cm.Parameters [ “@返回值”]值; Value的值为null。我找到的每一篇教程或文章,似乎都是正确的。在这一点上,我想我只是想念一些小事,需要另一双眼睛。我也尝试设置返回参数之前所有其他人作为一个职位,我发现在MS网站上说我需要,但无论它在哪里,它返回null。
必须将其移动到使用区块以外才能关闭阅读器。从来没有想到会是这样。谢谢! – phreak3eb 2011-03-23 23:14:33