我用一些参数创建了一个存储过程。这些是我的程序的参数:
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);
}
当我执行此函数/过程时,我得到一个异常
参数数组中索引0处的参数为空。
如何将值传递给invitationID
和invitationGUID
?
答案 0 :(得分:7)
当userID
或email
为null
时,您必须将参数值设置为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);
}