0

我有我的方法

 using (var helper = new DbHelper())
        {

            _commandText = "AddUpdateContactDetails";

            var parameter = new[]
                                {
                                    new SqlParameter("@Id", 0), // error here
                                    new SqlParameter("@CustomerId", id),
                                    new SqlParameter("@FirstName", customerDetails.FirstName),
                                    new SqlParameter("@LastName", customerDetails.LastName),
                                    new SqlParameter("@CompanyName", customerDetails.Company),
                                    new SqlParameter("@Designation", customerDetails.DesigId),
                                    new SqlParameter("@Title", customerDetails.Title),
                                    new SqlParameter("@Email", customerDetails.Email),
                                    new SqlParameter("@Phone", customerDetails.Phone),
                                };
            helper.ExecuteNonQuery(_commandText, CommandType.StoredProcedure, parameter);
        }

Sp低于

Alter Procedure AddUpdateContactDetails
(

@Id int,
@CustomerId int,
@FirstName varchar(50),
@LastName varchar(50),
@CompanyName varchar(150),
@Designation int,
@Title int,
@Email varchar(50),
@Phone varchar(50)
)
AS
    BEGIN
        IF @Id<=0
            BEGIN
                INSERT INTO CustomerDetails
                           (FirstName,CustomerId,LastName, CompanyName, Designation, Title,Email,Phone)
                     VALUES
                           (@FirstName,@CustomerId,@LastName,@CompanyName,@Designation,@Title,@Email,@Phone )

            END
        ELSE
            BEGIN
               UPDATE CustomerDetails
               SET FirstName= @FirstName ,
                  LastName= @LastName ,
                  CompanyName= @CompanyName ,
                  Designation= @Designation ,
                  Title= @Title ,
                  Email= @Email ,
                  Phone= @Phone 
                WHERE Id= @Id
            END

END

但仍然出现“过程或函数'AddUpdateContactDetails'需要参数'@Id'的异常,该参数未提供。”

4

1 回答 1

0

如果您要更新或插入的这个 talbel 没有 IDENTITY ,那么您的问题在于这行代码

IF @Id<=0
            BEGIN
                INSERT INTO CustomerDetails
                           (FirstName,CustomerId,LastName, CompanyName, Designation, Title,Email,Phone)
                     VALUES
                           (@FirstName,@CustomerId,@LastName,@CompanyName,@Designation,@Title,@Email,@Phone )

只需将 @id 添加到 Values 并将 Id 添加到您的 Insert 命令中,如下所示

IF @Id<=0
            BEGIN
                INSERT INTO CustomerDetails
                           (Id,FirstName,CustomerId,LastName, CompanyName, Designation, Title,Email,Phone)
                     VALUES
                           (@Id,@FirstName,@CustomerId,@LastName,@CompanyName,@Designation,@Title,@Email,@Phone )
于 2012-08-10T13:25:29.700 回答