将数据类型varchar转换为int时出错

时间:2013-05-10 21:11:13

标签: c# sql sql-server stored-procedures ado.net

这是我的存储过程代码

ALTER procedure [Proc_Add_User]
    (@UserId varchar(20), 
     @UserName varchar(100),
     @Page_Name varchar(20), 
     @AccessIndicator int, 
     @CreatedBy varchar(50), 
     @returnStatus varchar(50) output)
as
   DECLARE @intErrorCode INT
DECLARE @Page_Indicator INT
begin
BEGIN TRAN
Set @Page_Indicator = (select Page_Indicator from Pages where Page_Name=@Page_Name);
if (select count(*) from Users where UserId=@UserId and UserName=@UserName) > 0 begin               
    if (select count(*) from User_Credentials where Page_Indicator=@Page_Indicator and 
    UserId=@UserId  ) > 0 
    set @returnStatus='User already has access'             
    else 
    insert into User_Credentials(UserId,Page_Indicator,Access_Indicator,CreatedBy) 
        values (@UserId,@Page_Indicator,@AccessIndicator,@CreatedBy)
    SELECT @intErrorCode = @@ERROR
    IF (@intErrorCode <> 0) GOTO PROBLEM            
end
else begin  
    insert into Users(UserId,UserName,CreatedBy) 
    values(@UserId,@UserName,@CreatedBy)
    SELECT @intErrorCode = @@ERROR
    IF (@intErrorCode <> 0) GOTO PROBLEM
    insert into User_Credentials(UserId,Page_Indicator,Access_Indicator,CreatedBy) 
        values (@UserId,@Page_Indicator,@AccessIndicator,@CreatedBy)
    SELECT @intErrorCode = @@ERROR
    IF (@intErrorCode <> 0) GOTO PROBLEM    
end
COMMIT TRAN
if(@returnStatus is null)
    set @returnStatus='Success';
PROBLEM:
IF (@intErrorCode <> 0) BEGIN
set @returnStatus= 'Unexpected error occurred!'
ROLLBACK TRAN
end
end

我从下面粘贴的代码中调用它:

Con.Open();
cmd = new OleDbCommand();
cmd.Connection = Con;

cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "Proc_Add_User";

cmd.Parameters.Clear();
cmd.Parameters.Add("@UserId", SqlDbType.VarChar).Value = userLanId;
cmd.Parameters.Add("@UserName", SqlDbType.VarChar).Value = userName;
cmd.Parameters.Add("@Page_Name", SqlDbType.VarChar).Value = pageName;
cmd.Parameters.Add("@AccessIndicator", SqlDbType.Int).Value = accessIndicator;
cmd.Parameters.Add("@CreatedBy", SqlDbType.VarChar).Value = createdBy;

OleDbParameter output = new OleDbParameter("@returnStatus", SqlDbType.VarChar);
output.Direction = ParameterDirection.Output;
cmd.Parameters.Add(output); 

int result = cmd.ExecuteNonQuery();

我收到了ExecuteNonQuery声明中提到的错误。令我感到困惑的是,我能够在SSMS中执行存储过程,但不能从我的应用程序(前端)执行。我也提供了相同的值,但是我的应用程序失败了。

我仔细检查以确保传递的参数顺序匹配并且具有相同的数据类型,但仍然会抛出此错误。如果需要,我可以将我存储的proc代码粘贴到此处,所以请让我知道。谢谢!谢谢!

修改

OOPS!我刚刚意识到所有的插入都在发生,并且在数据库中得到了很好的提交。只是这个错误被我的应用程序中的catch块捕获。有任何想法吗?

我不能忽略它,因为基于ExecuteNonQuery()的返回值,我有一些语句,并且它不会通过ExecuteNonQuery()之后的代码。

1 个答案:

答案 0 :(得分:1)

这很可能是因为您SqlDbType使用了OleDbParameters

OleDbParameter output = new OleDbParameter("@returnStatus", SqlDbType.VarChar);

这会导致.NET使用OleDbParameter(String, Object)构造函数,将参数的值设置为SqlDbType.VarChar,假定它是int

你应该改用:

OleDbParameter output = new OleDbParameter("@returnStatus", OleDbType.VarChar);

并将您的来电更改为cmd.Parameters.Add以使用OleDbType

或者,您可以使用System.Data.SqlClient代替OleDb