c#调用存储过程的返回值总是-1
c#调用存储总是返回-1,数据执行并未成功。
查过资料,说是存储有问题,可是我用Sql2005直接运行存储赋值,则是正常的,还是先看代码吧。
存储代码(SQL2005)
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER PROCEDURE [dbo].[ActivedCode]
(
@checkcode nvarchar(4000),
@UserName nvarchar(4000),
@t int
)
AS
IF @t=0 --写入激活码
BEGIN
insert into [CheckCode] (UserName,CheckCode) Values ('' + @UserName + '',''+ @checkcode + '')
END
ELSE --激活帐户
BEGIN
--判断激活码是否已经使用了
declare @c int
declare @sqls nvarchar(4000)
set @sqls='select @a=Count(*) From [CheckCode] Where CheckCode =''' + @checkcode + ''' and isUsed=0'
exec sp_executesql @sqls,N'@a int output',@c output
if @c=1
BEGIN
--首先将激活码的状态改为已经使用
Update [CheckCode] Set IsUsed=1 Where CheckCode = @checkcode
--激活帐户
Update [Users] Set isLock=0 Where UserName = @UserName
END
END
//激活帐户
public string ActivePassport(string UserName, string ActiveCode)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConnectionString();
string s;
//s = false;
try
{
conn.Open();
DataSet ds = new DataSet();
SqlCommand u = new SqlCommand();
u.Connection = conn;
u.CommandType = CommandType.Text;
u.CommandText = "ActivedCode";//调用存储过程
u.Connection = conn;
u.CommandType = CommandType.StoredProcedure;
var _with1 = u.Parameters;
_with1.AddWithValue("@UserName", UserName);
_with1.AddWithValue("@checkcode", ActiveCode);
_with1.AddWithValue("@t",1);
if (u.ExecuteNonQuery() == 1)
{
s = "成功";
}
else
{
s = "失败" + u.ExecuteNonQuery();
}
}
catch(Exception ex)
{
s = "ok" + ex.Message ;
}
finally
{
conn.Close();
conn.Dispose();
}
return s.ToString();
}
USE [91listenData]
GO
DECLARE@return_value int
EXEC@return_value = [dbo].[ActivedCode]
@checkcode = N'13-68-FC-4F-99-E4-4C-8D-48-1F-C3-9B-7A-85-D9-96',
@UserName = N'91listen',
@t = 1
SELECT'Return Value' = @return_value
GO
SqlCommand u = new SqlCommand();
//这两行没有用了
u.Connection = conn;
u.CommandType = CommandType.Text;
u.CommandText = "ActivedCode";//调用存储过程
u.Connection = conn;
u.CommandType = CommandType.StoredProcedure;