无法通过将参数传递给存储过程来更新外键

时间:2013-01-17 14:02:34

标签: c# asp.net foreign-keys

我有一个应用程序可以更改表中的字段。这是一个简单的表单,用户可以更改与组关联的公司。

.NET

 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中,如果我对更新的值进行硬编码,那么它可以正常工作,但是如果我传递的值不是。我已经看了好几年了,但还没到任何地方。当我使用断点检查时,我的应用程序中的参数包含值

1 个答案:

答案 0 :(得分:0)

请尝试使用下面的SqlParameter.SqlValue,而不是通过SqlParameter.Value设置参数值。

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