将数据从许多方法返回列表插入到C#中的数据库时出错

时间:2019-02-27 03:34:41

标签: winforms arraylist c#-4.0 entity-framework-6

在这里我需要部分代码的帮助,所以在这里:

我有6种方法,如下所示,它们解析传入的数据,然后将其作为列表返回,所以我的问题是将该列表数据发送到我的数据库表SerialNumber中,列表中的每个方法都是一个单独的字段,填写数据库列。

例如,解析材料将填充数据库materiallookupcode列,其他填充相同。

这是数据库表的图像 enter image description here

这是所有5种读取数据然后返回数据的方法的代码,我需要将此数据发送到我的数据库

private List<string> ParseMaterial()
        {
            var materialList = new List<string>();

            foreach (var material in _connection.GetBarcodeList())
            {
                materialList.Add(material.Substring(10, 5));
            }

            return materialList;
        }

        private List<string> ParseLot()
        {
            var lotList = new List<string>();
            var establishmentList = GetEstablishmentCode();

            foreach (var lot in _connection.GetBarcodeList())
            {
                if (establishmentList.Contains("038"))
                {
                    lotList.Add(lot.Substring(28, 6) + _lotEstablishment.LoganSport038Property);
                }
                if (establishmentList.Contains("072"))
                {
                    lotList.Add(lot.Substring(28, 6) + _lotEstablishment.LouisaCounty072Property);
                }
                if (establishmentList.Contains("086"))
                {
                    lotList.Add(lot.Substring(28, 6) + _lotEstablishment.Madison086Property);
                }
                if (establishmentList.Contains("089"))
                {
                    lotList.Add(lot.Substring(28, 6) + _lotEstablishment.Perry089Property);
                }
                if (establishmentList.Contains("069"))
                {
                    lotList.Add(lot.Substring(28, 6) + _lotEstablishment.StormLake069Property);
                }
                if (establishmentList.Contains("088"))
                {
                    lotList.Add(lot.Substring(28, 6) + _lotEstablishment.Waterloo088Property);
                }
                if (establishmentList.Contains("265"))
                {
                    lotList.Add(lot.Substring(28, 6) + _lotEstablishment.GoodLetsVille265Property);
                }
                if (establishmentList.Contains("087"))
                {
                    lotList.Add(lot.Substring(28, 6) + _lotEstablishment.CouncilBluffs087Property);
                }
                if (establishmentList.Contains("064"))
                {
                    lotList.Add(lot.Substring(28, 6) + _lotEstablishment.Sherman064Property);
                }

            }

            return lotList;
        }

        private List<string> ParseSerialNumber()
        {
            var serialNumberList = new List<string>();

            foreach (var serialNumber in _connection.GetBarcodeList())
            {
                serialNumberList.Add(serialNumber.Substring(36, 10));
            }

            return serialNumberList;
        }

        public List<string> ParseNetWeight()
        {
            var netWeightList = new List<string>();


            foreach (var netWeight in _connection.GetBarcodeList())
            {            
                netWeightList.Add(netWeight.Substring(22, 4));
            }

            return netWeightList;
        }

        public List<string> ParseGrossWeight()
        {
            var grossWeightList = new List<string>();

            foreach (var grossWeight in _connection.GetBarcodeList())
            {
                grossWeightList.Add(grossWeight.Substring(22, 4));
            }

            return grossWeightList;
        }

        public List<string> FullBarcode()
        {
            var receiveFullBarcodeList = new List<string>();

            foreach (var fullBarcode in _connection.GetBarcodeList())
            {
                receiveFullBarcodeList.Add(fullBarcode);
            }

            return receiveFullBarcodeList;
        }

        public List<string> GetEstablishmentCode()
        {
            var establishmentList = new List<string>();

            foreach (var establishmentCode in _connection.GetBarcodeList())
            {
                establishmentList.Add(establishmentCode.Substring(36, 3));
            }

            return establishmentList;
        }

问题是这里的按钮单击时将读取所有5种方法并将其发送到数据库,我确定将变量列表制成字符串的部分和分隔符部分是错误的,所以我需要如何将这些列表添加到数据库的每一列的正确方法

    private async void btn_SubmitData_Click(object sender, EventArgs e)
              {
                 // parse list methodss
            var materialList = ParseMaterial();
            var lotList = ParseLot();
            var netWeightList = ParseNetWeight();
            var grossWeightList = ParseGrossWeight();
            var serialNumberList = ParseSerialNumber();
            var fullSerialNumberList = FullBarcode();

            var material = "";
            var lot = "";
            var net = "";
            var gross = "";
            var serial = "";
            var fullSerial = "";

            var currentUser = _currentUser.GetCurrentUsernameOnApp();
            var licensePlateId = GetLicensePlateIds();

            for (var i = 0; i < _connection.GetBarcodeList().Count; i++)
            {
                 material = materialList[i];
                 lot = lotList[i];
                 net = netWeightList[i];
                 gross = grossWeightList[i];
                 serial = serialNumberList[i];
                 fullSerial = fullSerialNumberList[i];
            }

            // database table and columns
            var serialNumbersInsert = new List<SerialNumber>
            {
                new SerialNumber
                {
                    SerialNumberLookupCode = serial,
                    NetWeight = Convert.ToDecimal(net) / 100,
                    GrossWeight = Convert.ToDecimal(gross) / 100,
                    LotLookupCode = lot,
                    MaterialLookupCode = material,
                    FullSerialNumberLookupCode = fullSerial,
                    CreatedSysDateTime = DateTime.Now,
                    ModifiedSysDateTime = DateTime.Now,
                    CreatedSysUser = currentUser,
                    ModifiedSysUser = currentUser,
                    LicensePlateId = licensePlateId
                }
            };

            // insert to the database
            foreach (var list in serialNumbersInsert)
            {
                _unitOfWork.SerialNumbers.Add(list);
            }
            await _unitOfWork.Complete();
}

这里是SerialNumber域类,它使用代码优先迁移来表示数据库表

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BarcodeReceivingApp.Core.Domain
{
    // domain class, represents a database table in sql server using code 
    // first migration
    public class SerialNumber
    {
        public int Id { get; set; }
        public int LicensePlateId { get; set; }
        public string FullSerialNumberLookupCode { get; set; }
        public string SerialNumberLookupCode { get; set; }
        public decimal NetWeight { get; set; }
        public decimal GrossWeight { get; set; }
        public string LotLookupCode { get; set; }
        public string MaterialLookupCode { get; set; }
        public DateTime CreatedSysDateTime { get; set; }
        public DateTime ModifiedSysDateTime { get; set; }
        public string CreatedSysUser { get; set; }
        public string ModifiedSysUser { get; set; }

    }
}

我在其他地方进行搜索,但到目前为止找不到好的解决方案,因此对您有所帮助。

1 个答案:

答案 0 :(得分:0)

我能够解决我的问题,我要做的是在循环中分配所有列表,然后将它们分配到数据库中的每一列。

但是我仍在寻找一种更好,更干净的方法来解决此问题

private async void btn_SubmitData_Click(object sender, EventArgs e)
    {
        // parse list methods - represents each field of the database column
        var materialList = ParseMaterial();
        var lotList = ParseLot();
        var netWeightList = ParseNetWeight();
        var grossWeightList = ParseGrossWeight();
        var serialNumberList = ParseSerialNumber();
        var fullSerialNumberList = FullBarcode();
        var currentUser = _currentUser.GetCurrentUsernameOnApp();
        var licensePlateId = GetLicensePlateIds();


        for (var i = 0; i < _connection.GetBarcodeList().Count; i++)
        {
             var serialNumbersInsert = new List<SerialNumber>
             {
                 new SerialNumber
                 {
                     SerialNumberLookupCode = materialList[i],
                     NetWeight = Convert.ToDecimal(netWeightList[i]) / 100,
                     GrossWeight = Convert.ToDecimal(grossWeightList[i]) / 100,
                     LotLookupCode = lotList[i],
                     MaterialLookupCode = materialList[i],
                     FullSerialNumberLookupCode = fullSerialNumberList[i],
                     CreatedSysDateTime = DateTime.Now,
                     ModifiedSysDateTime = DateTime.Now,
                     CreatedSysUser = currentUser,
                     ModifiedSysUser = currentUser,
                     LicensePlateId = licensePlateId
                 }
             };

             foreach (var list in serialNumbersInsert)
             {
                 _unitOfWork.SerialNumbers.Add(list);
             }
             await _unitOfWork.Complete();
        }

    }