【发布时间】:2020-08-07 18:54:16
【问题描述】:
我有一个程序,它从 Excel 文件中获取混合数据表并将其放入 SQL Server 数据库中。它已经运行得相对较快,但我希望让它更有活力。目前,我使用OleDb 从 Excel 文件中选择数据,使用Entity Framework Core 将数据添加到 SQL Server 数据库。
我为 Excel 文件中的每个工作表都有一个类。我将在这里用作示例的类是我的vHost 类。在我使用OleDb 从vHost Excel 表中选择所有数据(使用SELECT *)后,我将数据放入Data Table 对象中。然后,我遍历数据表中的每一列,以在 Excel 表中创建列名的List<string> 对象。这些对于程序了解将数据插入 SQL Server 数据库的位置至关重要。一旦创建了列名列表,程序就会对Data Table 中的所有数据进行索引,并创建一个vHost 对象以放置在SQL Server 数据库中。这是我希望使其更具活力的部分。
为了让程序决定vHost 类中的哪个对象将某些数据设置为相等,它会将列列表中的索引与静态字符串进行比较。如果我们所在的列索引相等或包含相同的列名字符串,则与vHost 类中的对象相关的对象被设置为等于某些数据。这部分很长而且很难打字,但它允许我创建一个运行错误最少的程序。
这是我的 vHost 部分的代码:
public static int Insert(string Sheet, OleDbConnection conn, int assessment_id, int objectCount)
{
//OleDb selects all the data inside the vHost sheet and places it inside a DataTable object
OleDbCommand command = new OleDbCommand("SELECT * FROM [" + Sheet + "$]", conn);
DataTable Data = new DataTable();
OleDbDataAdapter adapter = new OleDbDataAdapter(command);
adapter.Fill(Data);
//A for loop is used to add the column names into a List<string> object for reference
List<string> ColumnNames = new List<string>();
for (int i = 0; i < Data.Columns.Count; i++)
{
ColumnNames.Add(Data.Columns[i].ColumnName.ToUpper());
}
using (var context = new DataWarehouseContext()) //Allows access to Database Migrations
{
foreach (DataRow dataRow in Data.Rows) //Goes into each Row in the vHost DataTable
{
RvtoolsVHost vHost = new RvtoolsVHost();
for (int i = 0; i < dataRow.ItemArray.Length; i++) //Indexes through each item in the Rows
{
//Whatever the index of 'i' can be used to check the ColumnName of the object with the list object
//Need to check if vHost Migration has an object for row item we want to import
//Decide which item in a vHost object is being currently accessed and add it into the vHost object
if (ColumnNames[i].Equals("HOST"))
{
vHost.Name = dataRow[i].ToString();
}
if (ColumnNames[i].Contains("DATACENTER")) //For the Datacenter_ID in vHost
{
try
{
vHost.DatacenterId = vDatacenter.GetID(dataRow[i].ToString(), assessment_id);
}
catch (Exception)
{
}
}
if (ColumnNames[i].Contains("CLUSTER")) //For the Cluster_ID in vHost
{
try
{
vHost.VClusterId = vCluster.GetID(dataRow[i].ToString(), assessment_id);
}
catch (Exception)
{
vHost.VClusterId = null;
}
}
if (ColumnNames[i].Contains("CONFIG STATUS"))
{
vHost.ConfigStatus = dataRow[i].ToString();
}
if (ColumnNames[i].Contains("CPU MODEL"))
{
vHost.CpuModel = dataRow[i].ToString();
}
if (ColumnNames[i].Contains("SPEED"))
{
vHost.Speed = Convert.ToInt32(dataRow[i]);
}
if (ColumnNames[i].Contains("HT AVAILABLE"))
{
vHost.HtAvailable = bool.Parse(dataRow[i].ToString());
}
if (ColumnNames[i].Contains("HT ACTIVE"))
{
vHost.HtActive = bool.Parse(dataRow[i].ToString());
}
if (ColumnNames[i].Contains("# CPU"))
{
vHost.NumCpus = Convert.ToInt32(dataRow[i]);
}
if (ColumnNames[i].Contains("CORES PER CPU"))
{
vHost.CoresPerCpu = Convert.ToInt32(dataRow[i]);
}
if (ColumnNames[i].Contains("# CORES"))
{
vHost.NumCpuCores = Convert.ToInt32(dataRow[i]);
}
if (ColumnNames[i].Contains("CPU USAGE %"))
{
vHost.CpuUsage = Convert.ToInt32(dataRow[i]);
}
if (ColumnNames[i].Contains("# MEMORY"))
{
vHost.NumMemory = Convert.ToInt32(dataRow[i]);
}
if (ColumnNames[i].Contains("MEMORY USAGE %"))
{
vHost.MemoryUsage = Convert.ToInt32(dataRow[i]);
}
if (ColumnNames[i].Contains("CONSOLE"))
{
vHost.Console = Convert.ToInt32(dataRow[i]);
}
if (ColumnNames[i].Contains("# NICS"))
{
vHost.NumNics = Convert.ToInt32(dataRow[i]);
}
if (ColumnNames[i].Contains("# HBAS"))
{
vHost.NumHbas = Convert.ToInt32(dataRow[i]);
}
if (ColumnNames[i].Contains("# VMS"))
{
vHost.NumVms = Convert.ToInt32(dataRow[i]);
}
if (ColumnNames[i].Contains("VMS PER CORE"))
{
vHost.VmsPerCore = Convert.ToInt32(dataRow[i]);
}
if (ColumnNames[i].Contains("# VCPUS"))
{
vHost.NumVCpus = Convert.ToInt32(dataRow[i]);
}
if (ColumnNames[i].Contains("VCPUS PER CORE"))
{
vHost.VCpusPerCore = Convert.ToInt32(dataRow[i]);
}
if (ColumnNames[i].Contains("VRAM"))
{
vHost.VRam = Convert.ToInt32(dataRow[i]);
}
if (ColumnNames[i].Contains("VM USED MEMORY"))
{
vHost.VmUsedMemory = Convert.ToInt32(dataRow[i]);
}
if (ColumnNames[i].Contains("VM MEMORY SWAPPED"))
{
vHost.VmMemorySwapped = Convert.ToInt32(dataRow[i]);
}
if (ColumnNames[i].Contains("VM MEMORY BALLOONED"))
{
vHost.VmMemoryBallooned = Convert.ToInt32(dataRow[i]);
}
if (ColumnNames[i].Contains("VMOTION SUPPORT"))
{
vHost.VmotionSupport = bool.Parse(dataRow[i].ToString());
}
if (ColumnNames[i].Contains("STORAGE VMOTION SUPPORT"))
{
vHost.VmotionSupportStorage = bool.Parse(dataRow[i].ToString());
}
if (ColumnNames[i].Contains("CURRENT EVC"))
{
vHost.EvcCurrent = dataRow[i].ToString();
}
if (ColumnNames[i].Contains("MAX EVC"))
{
vHost.EvcMax = dataRow[i].ToString();
}
if (ColumnNames[i].Contains("ESX VERSION"))
{
vHost.EsxVersion = dataRow[i].ToString();
}
if (ColumnNames[i].Contains("BOOT TIME"))
{
vHost.BootTime = Convert.ToDateTime(dataRow[i].ToString());
}
if (ColumnNames[i].Contains("DNS SERVERS"))
{
vHost.DnsServers = dataRow[i].ToString();
}
if (ColumnNames[i].Contains("DHCP"))
{
vHost.Dhcp = bool.Parse(dataRow[i].ToString());
}
if (ColumnNames[i].Contains("DOMAIN"))
{
vHost.Domain = dataRow[i].ToString();
}
if (ColumnNames[i].Contains("DNS SEARCH ORDER"))
{
vHost.DnsSearchOrder = dataRow[i].ToString();
}
if (ColumnNames[i].Contains("NTP SERVER(S)"))
{
vHost.NtpServers = dataRow[i].ToString();
}
if (ColumnNames[i].Contains("NTPD RUNNING"))
{
vHost.NtpdRunning = bool.Parse(dataRow[i].ToString());
}
if (ColumnNames[i].Contains("TIME ZONE"))
{
vHost.TimeZone = dataRow[i].ToString();
}
if (ColumnNames[i].Contains("TIME ZONE NAME"))
{
vHost.TimeZoneName = dataRow[i].ToString();
}
if (ColumnNames[i].Contains("GMT OFFSET"))
{
vHost.GmtOffset = dataRow[i].ToString();
}
if (ColumnNames[i].Contains("VENDOR"))
{
vHost.Vendor = dataRow[i].ToString();
}
if (ColumnNames[i].Contains("MODEL"))
{
vHost.Model = dataRow[i].ToString();
}
if (ColumnNames[i].Contains("SERVICE TAG"))
{
vHost.ServiceTag = dataRow[i].ToString();
}
if (ColumnNames[i].Contains("OEM SPECIFIC STRING"))
{
vHost.OemSpecificString = dataRow[i].ToString();
}
if (ColumnNames[i].Contains("BIOS VERSION"))
{
vHost.BiosVersion = dataRow[i].ToString();
}
if (ColumnNames[i].Contains("BIOS DATE"))
{
vHost.BiosDate = Convert.ToDateTime(dataRow[i].ToString());
}
if (ColumnNames[i].Contains("OBJECT ID"))
{
vHost.ObjectId = dataRow[i].ToString();
}
}
vHost.AssessmentId = assessment_id;
context.RvtoolsVHost.Add(vHost);
context.SaveChanges();
objectCount += 47;
}
return objectCount;
}
}
希望我的 cmets 也能帮助您理解该程序! ObjectCount 可以与 assessment_id 或任何 GetID() 函数一起忽略。
另外,如果这有帮助,这里是vHost class。它是由EF Core Migration 创建的:
public partial class RvtoolsVHost
{
public RvtoolsVHost()
{
Location = new HashSet<Location>();
RvtoolsVHba = new HashSet<RvtoolsVHba>();
RvtoolsVInfo = new HashSet<RvtoolsVInfo>();
RvtoolsVMultiPath = new HashSet<RvtoolsVMultiPath>();
RvtoolsVNic = new HashSet<RvtoolsVNic>();
RvtoolsVRp = new HashSet<RvtoolsVRp>();
RvtoolsVScVmk = new HashSet<RvtoolsVScVmk>();
VHostToVSwitch = new HashSet<VHostToVSwitch>();
}
[Key]
public int VHostId { get; set; }
public string Name { get; set; }
public string ConfigStatus { get; set; }
public string CpuModel { get; set; }
public int? Speed { get; set; }
public bool? HtAvailable { get; set; }
public bool? HtActive { get; set; }
public int? NumCpus { get; set; }
public int? CoresPerCpu { get; set; }
public int? NumCpuCores { get; set; }
public int? CpuUsage { get; set; }
public int? NumMemory { get; set; }
public int? MemoryUsage { get; set; }
public int? Console { get; set; }
public int? NumNics { get; set; }
public int? NumHbas { get; set; }
public int? NumVms { get; set; }
public int? VmsPerCore { get; set; }
public int? NumVCpus { get; set; }
public int? VCpusPerCore { get; set; }
public int? VRam { get; set; }
public int? VmUsedMemory { get; set; }
public int? VmMemorySwapped { get; set; }
public int? VmMemoryBallooned { get; set; }
public bool? VmotionSupport { get; set; }
public bool? VmotionSupportStorage { get; set; }
public string EvcCurrent { get; set; }
public string EvcMax { get; set; }
public string EsxVersion { get; set; }
public DateTime? BootTime { get; set; }
public string DnsServers { get; set; }
public bool? Dhcp { get; set; }
public string Domain { get; set; }
public string DnsSearchOrder { get; set; }
public string NtpServers { get; set; }
public bool? NtpdRunning { get; set; }
public string TimeZone { get; set; }
public string TimeZoneName { get; set; }
public string GmtOffset { get; set; }
public string Vendor { get; set; }
public string Model { get; set; }
public string ServiceTag { get; set; }
public string OemSpecificString { get; set; }
public string BiosVersion { get; set; }
public DateTime? BiosDate { get; set; }
public string ObjectId { get; set; }
public int DatacenterId { get; set; }
public int? VClusterId { get; set; }
public int AssessmentId { get; set; }
public virtual Assessment Assessment { get; set; }
public virtual Datacenter Datacenter { get; set; }
public virtual RvtoolsVCluster VCluster { get; set; }
public virtual ICollection<Location> Location { get; set; }
public virtual ICollection<RvtoolsVHba> RvtoolsVHba { get; set; }
public virtual ICollection<RvtoolsVInfo> RvtoolsVInfo { get; set; }
public virtual ICollection<RvtoolsVMultiPath> RvtoolsVMultiPath { get; set; }
public virtual ICollection<RvtoolsVNic> RvtoolsVNic { get; set; }
public virtual ICollection<RvtoolsVRp> RvtoolsVRp { get; set; }
public virtual ICollection<RvtoolsVScVmk> RvtoolsVScVmk { get; set; }
public virtual ICollection<VHostToVSwitch> VHostToVSwitch { get; set; }
}
如果有一种方法可以简化静态字符串比较的数量,或者任何人都能想到的 if 语句,那就太棒了!
【问题讨论】:
标签: c# visual-studio oop entity-framework-core oledb