简体   繁体   English

在 c# 和 mvc 中的类之间传递数据表

[英]Passing a data table between classes in c# and mvc

Its been awhile I have been programming in c# and have a question about creating a data table in a class and then trying to access that datatable from another class or mvc controller. Its been awhile I have been programming in c# and have a question about creating a data table in a class and then trying to access that datatable from another class or mvc controller. Some advice and some explanation would be much appreciated一些建议和一些解释将不胜感激

Current class电流 class

using OfficeOpenXml;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using ppms_excel.Models;



namespace ppms_excel.Models
{

    public class PODataTable
    {

        public DataTable LDataTable
        {
            get
            {
                DataTable legalEntityTable = new DataTable("legal_entitiy");
                legalEntityTable.Columns.Add("Id", typeof(int));
                legalEntityTable.Columns.Add("ExternalId", typeof(string));
                legalEntityTable.Columns.Add("Source", typeof(string));
                legalEntityTable.Columns.Add("XpressEntityId", typeof(int));
                legalEntityTable.Columns.Add("EntityTypeId", typeof(int));
                legalEntityTable.Columns.Add("EntityName", typeof(string));
                legalEntityTable.Columns.Add("EIN", typeof(string));
                legalEntityTable.Columns.Add("Descripton", typeof(string));
                legalEntityTable.Columns.Add("Address1", typeof(string));
                legalEntityTable.Columns.Add("Address2", typeof(string));
                legalEntityTable.Columns.Add("City", typeof(string));
                legalEntityTable.Columns.Add("State", typeof(string));
                legalEntityTable.Columns.Add("PostalCode", typeof(string));
                legalEntityTable.Columns.Add("WebSiteUrl", typeof(string));
                legalEntityTable.Columns.Add("PricingAgreementTime", typeof(DateTimeOffset));
                legalEntityTable.Columns.Add("AgreementTime", typeof(DateTimeOffset));
                legalEntityTable.Columns.Add("AgreementSoourceIP", typeof(string));
                legalEntityTable.Columns.Add("CustomerServiceEmail", typeof(string));
                legalEntityTable.Columns.Add("CustomerServicePhoneNumber", typeof(string));
                legalEntityTable.Columns.Add("Active", typeof(bool));
                legalEntityTable.Columns.Add("TimeZone", typeof(string));
                legalEntityTable.Columns.Add("CreateTime", typeof(DateTimeOffset));
                legalEntityTable.Columns.Add("RiskToken", typeof(string));
                legalEntityTable.Columns.Add("DBA", typeof(string));
                legalEntityTable.Columns.Add("YearOfFormation", typeof(string));
                legalEntityTable.Columns.Add("DateOfFormation", typeof(string));


                return legalEntityTable;
            }
        }
    }
}

Controller Controller

    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Extensions.Logging;
    using OfficeOpenXml;
    using ppms_excel.Models;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Diagnostics;
    using System.Linq;
    using System.Threading.Tasks;
    using System.IO;
    using Excel = Microsoft.Office.Interop.Excel;
    
    
    namespace ppms_excel.Controllers
    {
        public class LoadDataTableController1 : Controller
        {
            public IActionResult Index()
            {
    
                //var x=new PODataTable().LDataTable.TableName;
                
                
                ExcelPackage ld = new ExcelPackage();
                
    
    
                ExcelWorksheet worksheet = ld.Workbook.Worksheets.FirstOrDefault();
                if (worksheet == null)
                {
                    //return or alert message here
                }
                else
                {
    
                    
    
                    var colcount= worksheet.Dimension.Columns;
                    string[] coltitle = new string[colcount];
                    for (int i = 1; i <= colcount; i++)
                    {
                        coltitle[i - 1] = (string) worksheet.Cells[1, i].Value;
                    }
    
    
    
    
                       //read excel file data and add data in  model.StaffInfoViewModel.StaffList
                           var rowCount = worksheet.Dimension.Rows;
                           var colCount = worksheet.Dimension.Columns;
                           **PODataTable Le = new PODataTable().LDataTable;**
    
    
                  
                }
                return View();
            }
        }
    }

is this the correct way to get the datatable instantiated in the controller?这是在 controller 中实例化数据表的正确方法吗? PODataTable Le = new PODataTable().LDataTable; PODataTable Le = new PODataTable().LDataTable;

thanks谢谢

Sure, there are many ways to solve this.当然,有很多方法可以解决这个问题。 In general, it's always a good idea to separate controller logic from model or utility logic, like you have done.通常,将 controller 逻辑与 model 或实用程序逻辑分开总是一个好主意,就像您所做的那样。 The reference to ppms_excel.Models will allow you to use that class just like you have it.ppms_excel.Models的引用将允许您像拥有它一样使用 class。

In general, I would be careful about using properties for building and passing back more complicated objects.一般来说,我会小心使用属性来构建和传回更复杂的对象。 This can lead to poor performance since the object will be recreated each time.这可能会导致性能下降,因为每次都会重新创建 object。 If possible, store the built object in a local variable of the utility class and return that instead.如果可能,将构建的 object 存储在实用程序 class 的局部变量中,然后将其返回。 This will prevent unnecessary recreates (if it will be the same each time).这将防止不必要的重新创建(如果每次都相同)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM