将数组的值插入到 c# 中的存储过程 EF 时出现问题和错误

Issue and Error while inserting values of an array to a stored procedure EF in c#

我在尝试将数据插入 5 个数组变量的存储过程时遇到问题,我收到的错误如下 System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index。你能看到我在这里做错了什么吗?变量 lotlist、netweightlist、grossweightlist 和 serialnumberlist 可以有一对多的数据被插入到我的存储过程中。

将创建我的存储过程的方法

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BarcodeReceivingApp.Persistence;
using BarcodeReceivingApp.Persistence.Repositories;

namespace BarcodeReceivingApp.Functionality
{
    public class StoredProcedureInsert
    {
        private readonly BarcodeReceivingFootPrintDbContext _barcodeReceivingFootPrintDbContext = new BarcodeReceivingFootPrintDbContext();

        public void CallManualBlindBarcodeParsingEventRequestFootPrintProcedure
        (
            decimal actualPackagedAmount, int actualPackagedPackId, string lotLookupCode,
            int warehouseId, int materialId, string vendorLotLookupCode, DateTime vendorLotManufactureDate, 
            DateTime vendorLotExpirationDate, int shipmentId, decimal netWeight, 
            decimal grossWeight, string serialLookupCode, string licensePlateLookupCode
        )
        {
            _barcodeReceivingFootPrintDbContext.Database
                .ExecuteSqlCommand
                ("EXEC noram_reporting.ManualBlindBarcodeParsingEventRequest " +
                 "@ActualPackagedAmount, @ActualPackagedPackId, @LotLookupCode, @WarehouseId, @MaterialId, @VendorLotLookupCode," +
                 "@VendorLotManufactureDate, @VendorLotExpirationDate, @ShipmentId, @netWeight, @grossWeight, @serialLookupCode, @licensePlateLookupCode",
      new SqlParameter("@ActualPackagedAmount", actualPackagedAmount),
                    new SqlParameter("@ActualPackagedPackId", actualPackagedPackId),
                    new SqlParameter("@LotLookupCode", lotLookupCode),
                    new SqlParameter("@WarehouseId", warehouseId),
                    new SqlParameter("@MaterialId", materialId),
                    new SqlParameter("@VendorLotLookupCode", vendorLotLookupCode),
                    new SqlParameter("@VendorLotManufactureDate", vendorLotManufactureDate),
                    new SqlParameter("@VendorLotExpirationDate", vendorLotExpirationDate),
                    new SqlParameter("@ShipmentId", shipmentId),
                    new SqlParameter("@netWeight", netWeight),
                    new SqlParameter("@grossWeight", grossWeight),
                    new SqlParameter("@serialLookupCode", serialLookupCode),
                    new SqlParameter("@licensePlateLookupCode", licensePlateLookupCode)
                    );
        }
    }
}

这是调用该方法将数据插入我的存储过程的方法

private void SendStoredProcedureDataToFootPrint()
        {
            var lotList = _connection.ParseLot();
            var netWeightList = _connection.ParseNetWeight();
            var grossWeightList = _connection.ParseGrossWeight();
            var serialNumberList = _connection.ParseSerialNumber();
            var material = _unitOfWork.Shipments.GetLastMaterialEntry();
            var scanCounts = _connection.CountReceivingBarcodeEntries();
            var packagingId = _unitOfWork.Materials.GetPackagingId();
            var warehouse = _unitOfWork.Warehouses.GetWarehouseIdQuery();
            var shipment = _unitOfWork.Shipments.GetLastShipmentIdEntry();
            var licensePlate = _unitOfWork.LicensePlates.GetLastCreatedLicensePlate();


            try
            {
                for (var i = 0; i <= _connection.GetBarcodeList().Count; i++)
                {
                    _storedProcedureInsert.CallManualBlindBarcodeParsingEventRequestFootPrintProcedure
                    (
                        scanCounts,
                        packagingId,
                        lotList[i],
                        warehouse,
                        5785,
                        lotList[i],
                        DateTime.Now,
                        DateTime.Now,
                        shipment,
                        Convert.ToDecimal(netWeightList[i]) / 100,
                        Convert.ToDecimal(grossWeightList[i]) / 100,
                        serialNumberList[i],
                        licensePlate
                    );
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
                throw;
            }
        }

所以目标是在 for 循环中将数据插入到我的存储过程中,而不会有任何来自可以具有 1 个或多个值的数组变量的问题。

我尝试了其他来源,但没有找到任何帮助。

由于访问无效的数组索引,您似乎在 .NET 端遇到异常

for (var i = 0; i <= _connection.GetBarcodeList().Count; i++)

这里应该是i < _connection.GetBarcodeList().Count;吧?数组中元素的最后一个索引是 _connection.GetBarcodeList().Count - 1 所以我猜你这里有异常 lotList[i],