注册 登录
编程论坛 ASP.NET技术论坛

下面是存储过程哪里错误了呢

liaohongchu 发布于 2010-09-13 14:49, 606 次点击
调用存储过程
public int SelectCount(string strWhere)
  {
  int theValue;
  using (SqlConnection myConnection = new SqlConnection(UserCenterConfiguration.Default.DbConnectionString))
  {

  using (SqlCommand command = new SqlCommand("usp_Get_BlogAttentionCount", myConnection))
  {
   = CommandType.StoredProcedure;
  command.Parameters.Add("@count", SqlDbType.Int);
  command.Parameters.Add("@strWhere", SqlDbType.NVarChar, 500, strWhere);
  command.Parameters["@count"].Direction = ParameterDirection.Output;
  try
  {
  command.ExecuteNonQuery();
  theValue = (int)command.Parameters["@count"].Value;
  myConnection.Close();
  }
  catch
  {
  myConnection.Close();
  theValue = 0;
  }
  return theValue;
  }
  }
  }


下面是存储过程
ALTER procedure [dbo].[usp_Get_BlogAttentionCount]
(
@strWhere nvarchar(500)='',
@count int output
)
as
declare @sqlStr nvarchar(1000)
if @strWhere != ''
  set @sqlStr = N'select @COUNT = count(id) from BlogAttention where 1=1 ' + @strWhere  
else
  set @sqlStr = N'select @COUNT = count(id) from BlogAttention'

exec sp_executesql @sqlStr,N'@count int output',@count output
2 回复
#2
qingshuiliu2010-09-13 16:48
搞的有些复杂了
可能的错误地方,作为你的参考:
if @strWhere != '' ==》if len(@strWhere)>0 好一点
“exec sp_executesql @sqlStr,N'@count int output',@count output ” 这句不需要。
上面的set语句已经给@count赋值了。

#3
liaohongchu2010-09-14 08:15
谢谢 你找到原因了 没有这个打开myConnection.open();

command.ExecuteNonQuery();
  theValue = (int)command.Parameters["@count"].Value;
  myConnection.Close();
1