1

对于存储过程,我收到以下异常/错误。

AL-DMK2_1“将 varchar 值转换为数据类型 int时转换失败。”

存储过程:

ALTER PROCEDURE dbo.sp_generateNewRequestId
(
@Customer varchar(50),
@Network_Type varchar(25),
@Market_Name varchar(50),

@NewRequestId varchar(50) OUTPUT
)

AS

Begin

Declare @MarketCode as varchar(10);
Declare @CustomerPrefix as varchar(10);

SET @MarketCode = (select Market_Code from Customer_Market_Details where Customer =     @Customer and Network_Type = @Network_Type and Market_Name = @Market_Name);

SET @CustomerPrefix = (select Customer_Prefix from Customer_Market_Details where Customer = @Customer and Network_Type = @Network_Type and Market_Name = @Market_Name);

Declare @RequestIDPrefix as varchar(20);

SET @RequestIdPrefix =  @CustomerPrefix + @MarketCode + '_';

-- Count how many requests you already have with the same request ID Prefix.

Declare @RequestCount as int;

SET @RequestCount = (Select count (*) from request_details where request_id like   @RequestIdPrefix + '%');
SET @NewRequestId = @RequestIdPrefix + CAST ((@RequestCount + 1) AS varchar);

While (Exists (Select * from request_details where request_id = @NewRequestId ) )
Begin
SET @RequestCount = @RequestCount + 1;
SET @NewRequestId = @RequestIdPrefix + CAST ((@RequestCount + 1) AS varchar);
end

RETURN @NewRequestId;

END

调用存储过程:

    using (SqlConnection connection = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["dbString"].ConnectionString))
    {
        using (SqlCommand command = new SqlCommand("dbo.sp_generateNewRequestId", connection))
        {
            command.Parameters.AddWithValue("@Customer", customer);
            command.Parameters.AddWithValue("@Network_Type", technology);
            command.Parameters.AddWithValue("@Market_Name", market);
            command.Parameters.Add("@NewRequestId", SqlDbType.VarChar).Direction = ParameterDirection.Output;
            command.Parameters["@NewRequestId"].Size = 50;
            command.CommandType = CommandType.StoredProcedure;
            connection.Open();
            command.ExecuteNonQuery();
            connection.Close();
         }
    }

观察:

当输出参数设置为@RequestCount 时,过程成功返回@RequestCount。

varchar但是,如果尝试输出任何其他值,则会抛出具有上述错误的异常。执行失败在行command.ExecuteNonQuery()

在线提供的解决方案均不适用于此错误。感谢你的帮助!

谢谢!

4

1 回答 1

5

如果您从存储过程返回任何内容,则必须能够将其转换为 an int,因为返回类型为SqlCommand.ExecuteNonQuery()( inthttp://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand 。 executenonquery.aspx)和RETURNSQL Server 存储过程中的语句只能用于返回整数值(http://msdn.microsoft.com/en-us/library/ms174998.aspx),因此为什么ExecuteNonQuery返回类型为int,否则它会object

你快到了,只需去掉存储过程中的 return 语句,它看起来像这样:

ALTER PROCEDURE dbo.sp_generateNewRequestId
(
@Customer varchar(50),
@Network_Type varchar(25),
@Market_Name varchar(50),

@NewRequestId varchar(50) OUTPUT
)

AS

Begin

Declare @MarketCode as varchar(10);
Declare @CustomerPrefix as varchar(10);

SET @MarketCode = (select Market_Code from Customer_Market_Details where Customer =     @Customer and Network_Type = @Network_Type and Market_Name = @Market_Name);

SET @CustomerPrefix = (select Customer_Prefix from Customer_Market_Details where Customer = @Customer and Network_Type = @Network_Type and Market_Name = @Market_Name);

Declare @RequestIDPrefix as varchar(20);

SET @RequestIdPrefix =  @CustomerPrefix + @MarketCode + '_';

-- Count how many requests you already have with the same request ID Prefix.

Declare @RequestCount as int;

SET @RequestCount = (Select count (*) from request_details where request_id like   @RequestIdPrefix + '%');
SET @NewRequestId = @RequestIdPrefix + CAST ((@RequestCount + 1) AS varchar);

While (Exists (Select * from request_details where request_id = @NewRequestId ) )
Begin
SET @RequestCount = @RequestCount + 1;
SET @NewRequestId = @RequestIdPrefix + CAST ((@RequestCount + 1) AS varchar);
end

END

并将您的 C# 代码修改为:

string newRequestID = "";

using (SqlConnection connection = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["dbString"].ConnectionString))
{
    using (SqlCommand command = new SqlCommand("dbo.sp_generateNewRequestId", connection))
    {
        command.Parameters.AddWithValue("@Customer", customer);
        command.Parameters.AddWithValue("@Network_Type", technology);
        command.Parameters.AddWithValue("@Market_Name", market);
        command.Parameters.Add("@NewRequestId", SqlDbType.VarChar).Direction = ParameterDirection.Output;
        command.Parameters["@NewRequestId"].Size = 50;
        command.CommandType = CommandType.StoredProcedure;
        connection.Open();
        command.ExecuteNonQuery();

        newRequestID = (string)command.Parameters["@NewRequestId"].Value;

        connection.Close();
     }
}
于 2013-05-23T15:47:14.437 回答