gpt4 book ai didi

c# - 将值传递给 ObjectParameter 变量

转载 作者:太空狗 更新时间:2023-10-30 00:33:01 26 4
gpt4 key购买 nike

我创建了一个带有一些参数的存储过程。这些是我的程序参数:

ALTER Procedure [dbo].[SaveInvitation]
(
@InvitationID INT OUTPUT,
@UserID INT,
@Email NCHAR(100),
@InvitationGUID UNIQUEIDENTIFIER OUTPUT
)
AS

我的function.cs:

public SaveInvitation_Result SaveInvitation(int userID,string email)
{
SaveInvitation_Result result = null;

var results = this.DataContext.SaveInvitation(null,userID,email,null);

if (results != null)
{
result = results.FirstOrDefault();
}

return result;
}

我正在将一些参数传递给我的函数 SaveInvitation,如下所示:

我的DataBaseModel.Designer.cs:

public ObjectResult<SaveInvitation_Result> SaveInvitation(ObjectParameter invitationID, Nullable<global::System.Int32> userID, global::System.String email, ObjectParameter invitationGUID)
{
ObjectParameter userIDParameter;
if (userID.HasValue)
{
userIDParameter = new ObjectParameter("UserID", userID);
}
else
{
userIDParameter = new ObjectParameter("UserID", typeof(global::System.Int32));
}

ObjectParameter emailParameter;
if (email != null)
{
emailParameter = new ObjectParameter("Email", email);
}
else
{
emailParameter = new ObjectParameter("Email", typeof(global::System.String));
}

return base.ExecuteFunction<SaveInvitation_Result>("SaveInvitation", invitationID, userIDParameter, emailParameter, invitationGUID);
}

当我执行这个函数/过程时我得到一个异常

The parameter at index 0 in the parameters array is null.

如何将值传递给 invitationIDinvitationGUID

最佳答案

userIDemailnull时,必须将参数的值设置为DBNull.Value :

public ObjectResult<SaveInvitation_Result> SaveInvitation(ObjectParameter invitationID, Nullable<global::System.Int32> userID, global::System.String email, ObjectParameter invitationGUID)
{
ObjectParameter userIDParameter;
if (userID.HasValue)
{
userIDParameter = new ObjectParameter("UserID", userID);
}
else
{
userIDParameter = new ObjectParameter("UserID", typeof(global::System.Int32));
userIDParameter.Value = DBNull.Value;
}

ObjectParameter emailParameter;
if (email != null)
{
emailParameter = new ObjectParameter("Email", email);
}
else
{
emailParameter = new ObjectParameter("Email", typeof(global::System.String));
emailParameter.Value = DBNull.Value;
}

return base.ExecuteFunction<SaveInvitation_Result>("SaveInvitation", invitationID, userIDParameter, emailParameter, invitationGUID);
}

关于c# - 将值传递给 ObjectParameter 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14037838/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com