簡體   English   中英

無法通過將參數傳遞給存儲過程來更新外鍵

[英]Can't update foreign key by passing parameter to stored procedure

我有一個更改表中字段的應用程序。 這是一種簡單的形式,用戶可以更改與組關聯的公司。

。凈

 public static string EditGroup(vw_Group_Model editGroup)
        {
            string outcome = "";
            if (editGroup.RowType == "ALF")
            {
                try
                {

                    using (SqlConnection conn = AlfOnlineConnection())
                    {
                        conn.Open();
                        UserManager.Models.vw_Group_Model model = new vw_Group_Model();
                        using (var command = new SqlCommand("sp_UserManager_EditGroup", conn))
                        {
                            command.CommandType = CommandType.StoredProcedure;
                            command.Parameters.Add("@CompanyId", SqlDbType.Int).SqlValue = editGroup.companyId;
                            command.Parameters.Add("@GroupId", SqlDbType.Int).SqlValue = editGroup.groupId;
                            //command.Parameters.Add("@CompanyName", SqlDbType.NVarChar).SqlValue = editGroup.selected_company;
                            //command.Parameters.Add("@GroupName", SqlDbType.NVarChar).SqlValue = editGroup.groupName;
                            command.Parameters.Add("@StartDate", SqlDbType.DateTime).SqlValue = editGroup.StartDate;
                            command.Parameters.Add("@EndDate", SqlDbType.DateTime).SqlValue = editGroup.EndDate;

                            switch (editGroup.IsActive)
                            {
                                case true:
                                    command.Parameters.Add("@isactive", SqlDbType.TinyInt).SqlValue = 1;
                                    break;
                                case false:
                                    command.Parameters.Add("@isactive", SqlDbType.TinyInt).SqlValue = 0;
                                    break;
                            }

                            int rowsEdited = command.ExecuteNonQuery();

                            if (rowsEdited == 1)
                            {
                                outcome = "Row successfully edited.";
                            }
                        }
                    }
                }
                catch (SqlException ex)
                {
                    return ex.ToString();
                }
            }

T-SQL

USE [BradOnline]
GO
/****** Object:  StoredProcedure [dbo].[sp_UserManager_EditGroup]    Script Date: 01/17/2013 13:11:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO


ALTER PROCEDURE [dbo].[sp_UserManager_EditGroup] 
@GroupId INT, 
@CompanyId INT,
                                                 --@GroupName   NVARCHAR(50), 
                                                 --@CompanyName   NVARCHAR(50), 
                                                 @StartDate  DATETIME,
                                                 @EndDate  DATETIME,
                                                 @isactive TINYINT

AS 
  BEGIN 


  DECLARE @AccountManagerId uniqueidentifier
  SET @AccountManagerId = '7A1DC75D-2628-4F8D-A376-9382A0762568'
  --(SELECT G.AccountManagerId FROM dbo.aspnet_Custom_Groups AS G
        --                  WHERE Id = @GroupId)


  --DECLARE @CompanyId BIGINT
  --SET @CompanyId = (SELECT MAX(c.Id) FROM dbo.aspnet_Custom_Companies AS c
        --                  WHERE c.Name = @CompanyName)



  UPDATE 
    [dbo].[aspnet_Custom_Groups]
SET 
    --[Name] = @GroupName,
    [StartDate] = @StartDate,
    [EndDate] = @EndDate,
    [IsActive] = @isactive,
    [AccountManagerId] = NULL,
    [CompanyId] = @CompanyId
WHERE 
    [Id] = @GroupId
  END  

在SQL中,如果我對更新的值進行硬編碼,則它可以工作,但是如果我在其中傳遞值,則不行。 我已經看了很久了,但是什么也沒得到。 當我檢查斷點時,應用程序中的參數包含值

與其通過SqlParameter.SqlValue設置參數值, SqlParameter.SqlValue嘗試使用SqlParameter.Value如下所示。

command.Parameters.Add("@CompanyId", SqlDbType.Int).Value = editGroup.companyId;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM