Diskuze: CSV a ASP.NET

C# .NET .NET (C# a Visual Basic) CSV a ASP.NET American English version English version

Avatar
Maros2470
Člen
Avatar
Maros2470:

Zdravím, můžete mi někdo poradit odkaz, kde bych získal informace o importování dat z csv do databáze v ASP.NET aplikaci. Prohledal jsem kde co, ale našel jsem pouze export. Díky

 
Odpovědět 13.9.2014 9:28
Avatar
Maros2470
Člen
Avatar
Maros2470:

Zatím to mám napsáno takto, ale data to nenačte. Chybu to nehlásí žádnou. Jen to problikne a nic.

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using eli_elektro.Models;

namespace eli_elektro.Admin
{
    public partial class InsertCsv : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PopulateDatabaseData();
                lblMessage.Text = "Current Database Data.";
            }
        }

        private void PopulateDatabaseData()
        {
            using (var dc = new PriceContext())
            {
                gvData.DataSource = dc.Appliances.ToList();
                gvData.DataBind();
            }
        }

        public List<Appliance> GeAppliances()
        {
            var db = new PriceContext();
            var query = db.Appliances;
            return query.ToList();
        }


        protected void btnImportCSV_click(object sender, EventArgs e)
        {
            if (FileUpload1.PostedFile.ContentType == "text/csv" || FileUpload1.PostedFile.ContentType == "application/vnd.ms-excel")
            {
                string fileName = Path.Combine(Server.MapPath("~/UploadDocuments"), new Guid() + ".csv");
                FileUpload1.PostedFile.SaveAs(fileName);

                string[] lines = File.ReadAllLines(fileName);
                string[] fields;

                // Odstranění hlavičky CSV
                lines = lines.Skip(1).ToArray();
                var emList = new List<Appliance>();

                foreach (var line in lines)
                {
                    fields = line.Split(';');
                    emList.Add(item: new Appliance
                    {
                        Customer = fields[0],
                        Place1 = fields[1],
                        Place2 = fields[2],
                        Place3 = fields[3],
                        Place4 = fields[4],
                        ApplianceId = fields[5],
                        Name = fields[6],
                        SortingKey = fields[7],
                        InvertoryNumber = fields[8],
                        SerialNumber = fields[9],
                        CordLength = Convert.ToDecimal(fields[10]),
                        RevisionInterval = Convert.ToInt16(fields[11]),
                        TakenIntoEvidence = Convert.ToDateTime(fields[12]),
                        Note = fields[13],
                        Group = fields[14],
                        ProtectionClass = fields[15],
                        ConnectionMethod = fields[16],
                        AppCategory = fields[17],
                        EquipmentClass = fields[18],
                        UserValue0Name = fields[19],
                        UserValue0Value = fields[20],
                        UserValue1Name = fields[21],
                        UserValue1Value = fields[22],
                        UserValue2Name = fields[23],
                        UserValue2Value = fields[24],
                        UserValue3Name = fields[25],
                        UserValue3Value = fields[26],
                        UserValue4Name = fields[27],
                        UserValue4Value = fields[28],
                        UserValue5Name = fields[29],
                        UserValue5Value = fields[30],
                        UserValue6Name = fields[31],
                        UserValue6Value = fields[32],
                        UserValue7Name = fields[33],
                        UserValue7Value = fields[34],
                        UserValue8Name = fields[35],
                        UserValue8Value = fields[36],
                        UserValue9Name = fields[37],
                        UserValue9Value = fields[38],
                        MeasDate = Convert.ToDateTime(fields[39]),
                        MPr = fields[40],
                        MZk = fields[41],
                        MRpe200mA = fields[42],
                        MRpe10A = fields[43],
                        Mu10A = fields[44],
                        MRiso500V = fields[45],
                        MRiso250V = fields[46],
                        MRiso100V = fields[47],
                        MRisoInW = fields[48],
                        MRisoWPe = fields[49],
                        MRisoInPe = fields[50],
                        MIpe = fields[51],
                        MIpeInv = fields[52],
                        MIpeExt = fields[53],
                        MIdif = fields[54],
                        MIdifInv = fields[55],
                        MIdifExt = fields[56],
                        MId = fields[57],
                        MIdDif = fields[58],
                        MIsub = fields[59],
                        MidW = fields[60],
                        MIdDifW = fields[61],
                        Mp = fields[62],
                        MpExt = fields[63],
                        Ms = fields[64],
                        MsExt = fields[65],
                        McosPhi = fields[66],
                        Mi = fields[67],
                        Mu = fields[68],
                        MiExt = fields[69],
                        MTemp = fields[70],
                        MRpm = fields[71],
                        MUoVef = fields[72],
                        MUoVpp = fields[73],
                        MimSn = fields[74],
                        MimPn = fields[75],
                        MimPp = fields[76],
                        MimPPz = fields[77],
                        MeasInspectionNote = fields[78],
                        MeasInspectionStatus = fields[79],
                        MeasFunctTestNote = fields[80],
                        MeasFunctTestStatus = fields[81],
                        MeasMeasurementsNote = fields[82],
                        MeasMeasurementsStatus = fields[83],
                        MeasGlobalNote = fields[84],
                        MeasGlobalStatus = fields[85],
                        InspectorName = fields[86],
                        InspectorSecondName = fields[87],
                        InspectorLicense = fields[88],
                        DeviceSerialNum = fields[89],
                        DeviceType = fields[90],
                        DeviceCalibrationNum = fields[91],
                        DeviceCalibrationExpire = fields[92],
                        SupplFirm = fields[93],
                        SupplIc = fields[94],

                    });

                    // Update Database Data

                    using (var dc = new PriceContext())
                    {
                        foreach (var i in emList)
                        {
                            var v = dc.Appliances.FirstOrDefault(a => a.Id.Equals(i.Id));
                            if (v != null)
                            {

                                v.Customer = i.Customer;
                                v.Place1 = i.Place1;
                                v.Place2 = i.Place2;
                                v.Place3 = i.Place3;
                                v.Place4 = i.Place4;
                                v.ApplianceId = i.ApplianceId;
                                v.Name = i.Name;
                                v.SortingKey = i.SortingKey;
                                v.InvertoryNumber = i.InvertoryNumber;
                                v.SerialNumber = i.SerialNumber;
                                v.CordLength = i.CordLength;
                                v.RevisionInterval = i.RevisionInterval;
                                v.TakenIntoEvidence = i.TakenIntoEvidence;
                                v.Note = i.Note;
                                v.Group = i.Group;
                                v.ProtectionClass = i.ProtectionClass;
                                v.ConnectionMethod = i.ConnectionMethod;
                                v.AppCategory = i.AppCategory;
                                v.EquipmentClass = i.EquipmentClass;
                                v.UserValue0Name = i.UserValue0Name;
                                v.UserValue0Value = i.UserValue0Value;
                                v.UserValue1Name = i.UserValue1Name;
                                v.UserValue1Value = i.UserValue1Value;
                                v.UserValue2Name = i.UserValue2Name;
                                v.UserValue2Value = i.UserValue2Value;
                                v.UserValue3Name = i.UserValue3Name;
                                v.UserValue3Value = i.UserValue3Value;
                                v.UserValue4Name = i.UserValue4Name;
                                v.UserValue4Value = i.UserValue4Value;
                                v.UserValue5Name = i.UserValue5Name;
                                v.UserValue5Value = i.UserValue5Value;
                                v.UserValue6Name = i.UserValue6Name;
                                v.UserValue6Value = i.UserValue6Value;
                                v.UserValue7Name = i.UserValue7Name;
                                v.UserValue7Value = i.UserValue7Value;
                                v.UserValue8Name = i.UserValue8Name;
                                v.UserValue8Value = i.UserValue8Value;
                                v.UserValue9Name = i.UserValue9Name;
                                v.UserValue9Value = i.UserValue9Value;
                                v.MeasDate = i.MeasDate;
                                v.MPr = i.MPr;
                                v.MZk = i.MZk;
                                v.MRpe200mA = i.MRpe200mA;
                                v.MRpe10A = i.MRpe10A;
                                v.Mu10A = i.Mu10A;
                                v.MRiso500V = i.MRiso500V;
                                v.MRiso250V = i.MRiso250V;
                                v.MRiso100V = i.MRiso100V;
                                v.MRisoInW = i.MRisoInW;
                                v.MRisoWPe = i.MRisoWPe;
                                v.MRisoInPe = i.MRisoInPe;
                                v.MIpe = i.MIpe;
                                v.MIpeInv = i.MIpeInv;
                                v.MIpeExt = i.MIpeExt;
                                v.MIdif = i.MIdif;
                                v.MIdifInv = i.MIdifInv;
                                v.MIdifExt = i.MIdifExt;
                                v.MId = i.MId;
                                v.MIdDif = i.MIdDif;
                                v.MIsub = i.MIsub;
                                v.MidW = i.MidW;
                                v.MIdDifW = i.MIdDifW;
                                v.Mp = i.Mp;
                                v.MpExt = i.MpExt;
                                v.Ms = i.Ms;
                                v.MsExt = i.MsExt;
                                v.McosPhi = i.McosPhi;
                                v.Mi = i.Mi;
                                v.Mu = i.Mu;
                                v.MiExt = i.MiExt;
                                v.MTemp = i.MTemp;
                                v.MRpm = i.MRpm;
                                v.MUoVef = i.MUoVef;
                                v.MUoVpp = i.MUoVpp;
                                v.MimSn = i.MimSn;
                                v.MimPn = i.MimPn;
                                v.MimPp = i.MimPp;
                                v.MimPPz = i.MimPPz;
                                v.MeasInspectionNote = i.MeasInspectionNote;
                                v.MeasInspectionStatus = i.MeasInspectionStatus;
                                v.MeasFunctTestNote = i.MeasFunctTestNote;
                                v.MeasFunctTestStatus = i.MeasFunctTestStatus;
                                v.MeasMeasurementsNote = i.MeasMeasurementsNote;
                                v.MeasMeasurementsStatus = i.MeasMeasurementsStatus;
                                v.MeasGlobalNote = i.MeasGlobalNote;
                                v.MeasGlobalStatus = i.MeasGlobalStatus;
                                v.InspectorName = i.InspectorName;
                                v.InspectorSecondName = i.InspectorSecondName;
                                v.InspectorLicense = i.InspectorLicense;
                                v.DeviceSerialNum = i.DeviceSerialNum;
                                v.DeviceType = i.DeviceType;
                                v.DeviceCalibrationNum = i.DeviceCalibrationNum;
                                v.DeviceCalibrationExpire = i.DeviceCalibrationExpire;
                                v.SupplFirm = i.SupplFirm;
                                v.SupplIc = i.SupplIc;
                            }

                            else
                            {
                                dc.Appliances.Add(i);
                            }

                            dc.SaveChanges();
                            //Populate  Database Data
                            PopulateDatabaseData();
                            lblMessage.Text = "Data uložena!";
                        }
                    }
                }
            }
        }
    }
}
 
Nahoru Odpovědět 13.9.2014 9:32
Avatar
Nahoru Odpovědět 13.9.2014 9:44
Nikdy neříkej nahlas, že to nejde. Vždycky se totiž najde blbec, který to neví a udělá to...
Avatar
Nahoru Odpovědět 13.9.2014 9:45
Nikdy neříkej nahlas, že to nejde. Vždycky se totiž najde blbec, který to neví a udělá to...
Děláme co je v našich silách, aby byly zdejší diskuze co nejkvalitnější. Proto do nich také mohou přispívat pouze registrovaní členové. Pro zapojení do diskuze se přihlas. Pokud ještě nemáš účet, zaregistruj se, je to zdarma.

Zobrazeno 5 zpráv z 5.