我正在尝试执行我的存储过程,它需要 13 个参数,有些可以接受空值,有些总是需要的。我收到 2 个接受 null 的日期参数的错误。
我收到以下错误
System.Data.SqlClient.SqlException
参数化查询“(@recordType nvarchar(12),@lotCreation bit,@licensePlateCreation”需要参数“@lotManufactureDate”,但未提供该参数。
这是当我从 SQL Server 数据库调用存储过程时 edmx 模型创建的存储过程代码,这里licensePlateLookupCode
从存储过程命名的参数允许空值,但在 emdx 模型创建的代码中它不显示它可以采用空值- 你知道为什么吗?
它应该像Nullable<string> licensePlateLookupCode
而不像现在这样string licensePlateLookupCode
。
public virtual int AddFeedbackRequestsAgentInsert(string recordType,
Nullable<bool> lotCreation, Nullable<bool> licensePlateCreation,
Nullable<int> finishedGoodLineId, Nullable<int> lotid, string
lotLookupCode, Nullable<System.DateTime> lotManufactureDate,
Nullable<System.DateTime> lotExpirationDate, Nullable<decimal>
packagedAmount, Nullable<int> packagingId, string
licensePlateLookupCode, Nullable<int> licensePlateId, Nullable<int>
licensePlateLocationId)
{
var recordTypeParameter = recordType != null ?
new ObjectParameter("recordType", recordType) :
new ObjectParameter("recordType", typeof(string));
var lotCreationParameter = lotCreation.HasValue ?
new ObjectParameter("lotCreation", lotCreation) :
new ObjectParameter("lotCreation", typeof(bool));
var licensePlateCreationParameter = licensePlateCreation.HasValue ?
new ObjectParameter("licensePlateCreation", licensePlateCreation) :
new ObjectParameter("licensePlateCreation", typeof(bool));
var finishedGoodLineIdParameter = finishedGoodLineId.HasValue ?
new ObjectParameter("finishedGoodLineId", finishedGoodLineId) :
new ObjectParameter("finishedGoodLineId", typeof(int));
var lotidParameter = lotid.HasValue ?
new ObjectParameter("lotid", lotid) :
new ObjectParameter("lotid", typeof(int));
var lotLookupCodeParameter = lotLookupCode != null ?
new ObjectParameter("lotLookupCode", lotLookupCode) :
new ObjectParameter("lotLookupCode", typeof(string));
var lotManufactureDateParameter = lotManufactureDate.HasValue ?
new ObjectParameter("lotManufactureDate", lotManufactureDate) :
new ObjectParameter("lotManufactureDate", typeof(System.DateTime));
var lotExpirationDateParameter = lotExpirationDate.HasValue ?
new ObjectParameter("lotExpirationDate", lotExpirationDate) :
new ObjectParameter("lotExpirationDate", typeof(System.DateTime));
var packagedAmountParameter = packagedAmount.HasValue ?
new ObjectParameter("packagedAmount", packagedAmount) :
new ObjectParameter("packagedAmount", typeof(decimal));
var packagingIdParameter = packagingId.HasValue ?
new ObjectParameter("packagingId", packagingId) :
new ObjectParameter("packagingId", typeof(int));
var licensePlateLookupCodeParameter = licensePlateLookupCode != null ?
new ObjectParameter("licensePlateLookupCode", licensePlateLookupCode) :
new ObjectParameter("licensePlateLookupCode", typeof(string));
var licensePlateIdParameter = licensePlateId.HasValue ?
new ObjectParameter("licensePlateId", licensePlateId) :
new ObjectParameter("licensePlateId", typeof(int));
var licensePlateLocationIdParameter = licensePlateLocationId.HasValue ?
new ObjectParameter("licensePlateLocationId", licensePlateLocationId) :
new ObjectParameter("licensePlateLocationId", typeof(int));
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("AddFeedbackRequestsAgentInsert", recordTypeParameter, lotCreationParameter, licensePlateCreationParameter, finishedGoodLineIdParameter, lotidParameter, lotLookupCodeParameter, lotManufactureDateParameter, lotExpirationDateParameter, packagedAmountParameter, packagingIdParameter, licensePlateLookupCodeParameter, licensePlateIdParameter, licensePlateLocationIdParameter);
}
这是我调用该存储过程的地方,然后在用户单击提交按钮后执行它,我收到了 lotmanufacturer 和 lotexpiration 日期参数的错误,因为我认为它们是 NULLS,但在实际的数据库存储过程中它可以取空值或不
private void Btn_Submit_Click(object sender, EventArgs e)
{
// db context variable
var context = _manufacturingDbContext;
// ** Variables to insert to FootPrint stored procedure datex_footprint_integration.AddFeedbackRequestsAgentInsert with Record Type (FinishedGood)
const string recordType = "FinishedGood";
const bool lotCreation = false;
const bool licensePlateCreation = true;
var finishedGoodLineId = context.FinishedGoodLineIdByOrderAndFinishedGoodAndLot(Cmb_MfgOrder.Text, Cmb_FgLookupCode.Text, Cmb_LotLookupCode.Text).FirstOrDefault();
var lotId = context.LotIdByManufacturingOrderAndFinishedGood(Cmb_MfgOrder.Text, Cmb_FgLookupCode.Text,Cmb_LotLookupCode.Text).FirstOrDefault();
var doNotCreateLot = null;
DateTime? lotManufactureDate = null;
DateTime? lotExpirationDate = null;
var packagedAmount = Convert.ToDecimal(Txt_PackagedAmount.Text);
const int packagedId = 3;
var licensePlateLookupCode = Txt_LicensePlateLookupCode.Text;
int? licensePlateId = null;
const int licensePlateLocationId = 51372;
// Call SQL Server SPROC dbo.AddFeedbackRequestsAgentInsert and enter data to FootPrint Task
context.Database.ExecuteSqlCommand("EXEC dbo.AddFeedbackRequestsAgentInsert " +
"@recordType, @lotCreation, @licensePlateCreation, @finishedGoodLineId, @lotid, @lotLookupCode, @lotManufactureDate," +
"@lotExpirationDate, @packagedAmount, @packagingId, @licensePlateLookupCode, @licensePlateId, @licensePlateLocationId",
new SqlParameter("@recordType", recordType),
new SqlParameter("@lotCreation", lotCreation),
new SqlParameter("@licensePlateCreation", licensePlateCreation),
new SqlParameter("@finishedGoodLineId", finishedGoodLineId),
new SqlParameter("@lotid", lotId),
new SqlParameter("@lotLookupCode", doNotCreateLot),
new SqlParameter("@lotManufactureDate", lotManufactureDate),
new SqlParameter("@lotExpirationDate", lotExpirationDate),
new SqlParameter("@packagedAmount", packagedAmount),
new SqlParameter("@packagingId", packagedId),
new SqlParameter("@licensePlateLookupCode", licensePlateLookupCode),
new SqlParameter("@licensePlateId", licensePlateId),
new SqlParameter("@licensePlateLocationId", licensePlateLocationId)
);
context.SaveChanges();
}
这是实际的存储过程-您可以看到@lotManufactureDate
并且@lotExpirationDate
确实允许空值:
CREATE PROCEDURE [dbo].[AddFeedbackRequestsAgentInsert]
@recordType NVARCHAR(30),
@lotCreation BIT,
@licensePlateCreation BIT,
@finishedGoodLineId INT,
@lotid INT NULL,
@lotLookupCode NVARCHAR(256) NULL,
@lotManufactureDate DATETIME NULL,
@lotExpirationDate DATETIME NULL,
@packagedAmount DECIMAL(28,8),
@packagingId INT,
@licensePlateLookupCode NVARCHAR(256) NULL,
@licensePlateId INT NULL,
@licensePlateLocationId INT NULL
所以,我不明白为什么当我为那些具有空日期的 2 个日期参数传递时,我会收到预期的错误,如果我传递 null 时,如果 lotlookupcode 参数会发生同样的事情,我会得到与期待 lotlookupcode 相同的错误。你能看出这里可能是什么问题吗?
我对我的代码进行了新的更改,现在我没有像以前的请求描述那样得到错误,但是现在当我调用存储过程来执行时,我在数据库上看不到任何内容,可以根据参数在下面查看我提供的基于 emdx 模型是否正确?
我从模型浏览器导入函数存储过程中调用此函数,当我按下提交按钮时,我没有在数据库上收到任何内容,基于上面的 emdx 模型和存储过程,参数看起来是否正确?
var context = _manufacturingDbContext;
const string recordType = "FinishedGood";
const bool lotCreation = false;
const bool licensePlateCreation = true;
var finishedGoodLineId = context.FinishedGoodLineIdByOrderAndFinishedGoodAndLot(Cmb_MfgOrder.Text, Cmb_FgLookupCode.Text, Cmb_LotLookupCode.Text).FirstOrDefault();
var lotId = context.LotIdByManufacturingOrderAndFinishedGood(Cmb_MfgOrder.Text, Cmb_FgLookupCode.Text,Cmb_LotLookupCode.Text).FirstOrDefault();
var doNotCreateLot = null;
DateTime? lotManufactureDate = null;
DateTime? lotExpirationDate = null;
var packagedAmount = Convert.ToDecimal(Txt_PackagedAmount.Text);
const int packagedId = 3;
var licensePlateLookupCode = Txt_LicensePlateLookupCode.Text;
int? licensePlateId = null;
const int licensePlateLocationId = 51372;
//calling stored procedure and send data to sproc based on the variables above
context.AddFeedbackRequestsAgentInsert(recordType, lotCreation, licensePlateCreation, finishedGoodLineId,
lotId, lot, lotManufactureDate, lotExpirationDate, packagedAmount, packagedId, licensePlateLookupCode,
licensePlateId, licensePlateLocationId);
}