期望C#Sql参数,参数不在SP参数列表

时间:2016-09-14 15:53:59

标签: c# sql-server stored-procedures sqlparameter

获取“过程或函数”InsertAccount'需要参数'@AccountId',这是未提供的。“但是,存储过程在其参数列表中确实有此参数。

C#代码:

using (var connection = new SqlConnection(Settings.Default.InternalConnection))
            {
                connection.Open();
                using (var command = new SqlCommand { Connection = connection, CommandText = Settings.Default.AccountInsertRoutine, CommandType = CommandType.StoredProcedure, CommandTimeout = 120 })
                {
                    var parameter0 = new SqlParameter { ParameterName = "@AccountName", Value = Account.AccountName, SqlDbType = SqlDbType.VarChar, Direction = ParameterDirection.Input };
                    command.Parameters.Add(parameter0);

                    var parameter1 = new SqlParameter { ParameterName = "@AccountKey", Value = Account.AccountKey, SqlDbType = SqlDbType.VarChar, Direction = ParameterDirection.Input };
                    command.Parameters.Add(parameter1);

                    var parameter2 = new SqlParameter { ParameterName = "@AccountTypeId", Value = Account.AccountTypeId, SqlDbType = SqlDbType.Int, Direction = ParameterDirection.Input };
                    command.Parameters.Add(parameter2);

                    var parameter3 = new SqlParameter { ParameterName = "@FundId", Value = Account.FundId, SqlDbType = SqlDbType.Int, Direction = ParameterDirection.Input };
                    command.Parameters.Add(parameter3);

                    var parameter4 = new SqlParameter { ParameterName = "@Output", SqlDbType = SqlDbType.Int, Direction = ParameterDirection.Output };
                    command.Parameters.Add(parameter4);

                    command.ExecuteNonQuery();

                    Account.AccountId = parameter4.Value.ToInt32Default(0);
                }
            }

注意:

AccountInsertRoutine = dbo.InsertAccount

ToInt32Default(0);自定义例程将值转换为int32,如果错误或null则为0

帐户:包含AccountId,AccountName,AccountKey和FundId属性的类。

SQL Server存储过程:

CREATE PROCEDURE [dbo].[InsertAccount] (@AccountName  nvarchar(200), @AccountKey nvarchar(200), @AccountTypeId int, @fundId int, @Output int OUTPUT)
AS
    SET NOCOUNT ON;

    IF EXISTS(SELECT accountID FROM dbo.tblAccount WHERE sourceID = @AccountKey AND fundID = @fundId)
    BEGIN
        SELECT @Output = accountID FROM dbo.tblAccount WHERE sourceID = @AccountKey AND fundID = @fundId
    END
    ELSE
    BEGIN

        INSERT INTO dbo.tblAccount (accountName, fundID, sourceID, inceptionDate, taxIDNumber, typeID, erisaID, masterContactID, accountStatusID) 
            VALUES (@AccountName, @FundId, @AccountKey, NULL, NULL, @AccountTypeId, NULL, NULL, 1)

        SELECT @Output = SCOPE_IDENTITY();
    END
GO

我在这里缺少什么。

0 个答案:

没有答案