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

问题描述 投票:0回答:1

我在尝试将数据插入到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可以将1到多个数据插入到我的存储过程中。

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

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个或多个值的数组变量的任何问题。

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

c# arrays sql-server entity-framework ef-code-first
1个回答
2
投票

看起来你在.NET端有异常,因为访问了无效的数组索引

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

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

© www.soinside.com 2019 - 2024. All rights reserved.