学生表中的字段几个字段:用户名 varchar 10
存储过程为:作用是看指定的用户名在表中是否已经存在,若存在为,不存在为0
CREATE procedure check_if_only
@username varchar (10),
@checked smallint output
as
select   @checked=count(*)//赋值
from 学生
where 用户名=@username
select @checked
GO
asp.net页面中的代码为
myconnection=new  SqlConnection("server=10.0.60.231;uid=sa;pwd=;database=毕业设计");     SqlCommand mycommand=new  SqlCommand("check_if_only",myconnection);//存储过程
mycommand.CommandType = CommandType.StoredProcedure;
     SqlParameter para=mycommand.Parameters.Add("@username",SqlDbType.VarChar,10);
para.Value="a"; para=mycommand.Parameters.Add("@checked",SqlDbType.SmallInt,2);//@checked为输出参数,返回为0或者为1因为username是主键
para.Direction=ParameterDirection.Output;

myconnection.Open();
            SqlDataReader dr =mycommand.ExecuteReader(); int i=Convert.ToInt32(mycommand.Parameters["@checked"].Value);
Label14.Text=i.ToString(); 我在查询分析器里执行
declare @checked smallint
execute check_if_only '郑天妹',@checked output时能返回1,是正常的,因为表里有a的用户名
而在页面上执行时label14的值为0
不知道为什么

解决方案 »

  1.   

    如果是 Convert.ToInt32(null) 自然为0SqlCommand mycommand = new SqlCommand("check_if_only",myconnection);

    mycommand.CommandType = CommandType.StoredProcedure;mycommand.Parameters.Add("@checked",SqlDbType.SmallInt,2).Direction = ParameterDirection.Output;//......
    ......
    ......获取时用mycommand.Parameters["@checked"].Value.ToString()
      

  2.   

    xinyangt(信仰t) 
      我觉得页面是没什么错的,不知道存储过程有没有问题,因为我如果把存储过程这样改了就对了,CREATE procedure check_if_only
    @username varchar (10),
    @checked smallint output
    as
    if exists(select  用户名 from 学生 where 用户名 =@username)
    select @checked = 1
    else
    select @checked =0
    GO
    但是我觉得我原先那个存储过程也没什么错的,不知道为什么
      

  3.   

    http://goody9807.cnblogs.com/archive/2005/07/08/188475.html
    里面有一个分页存储过程 用到了输出参数!
      

  4.   

    修改下存储过程,给@checked赋初值
    CREATE procedure check_if_only
    @username varchar (10),
    @checked smallint=0 output
    as
    select   @checked=count(*)//赋值
    from 学生
    where 用户名=@username
    select @checked
    GO
      

  5.   

    to xiaolang88(海纳百川有容乃大) 
    我照你说的改了,还是不行