[英]Efficiency Suggestions for Moving Data from Excel to SQL Server Database using EF Core
我有一個程序,它從 Excel 文件中獲取混合數據表並將其放入 SQL 服務器數據庫中。 它已經運行得相對較快,但我希望讓它更有活力。 目前,我使用OleDb
到 Excel 文件中的 select 數據和Entity Framework Core
將數據添加到 SQL 服務器數據庫。
對於 Excel 文件中的每張紙,我都有一個 class。 我將在這里用作示例的 class 是我的vHost
class。 After I use OleDb
to select all the data from the vHost
Excel sheet (using SELECT *
) I place the data into a Data Table
object. 然后我通過數據表中的每一列 go 創建 Excel 表中列名的List<string>
object。 這些對於程序了解將數據插入 SQL 服務器數據庫的位置至關重要。 一旦創建了列名列表,程序就會對Data Table
中的所有數據進行索引,並創建一個vHost
object 以放置在 SQL 服務器數據庫中。 這是我希望使其更具活力的部分。
為了讓程序決定 vHost class 中的vHost
將某些數據設置為等於什么,它會將列列表中的索引與 static 字符串進行比較。 如果我們所在的列索引相等或包含相同的列名稱字符串,則與 vHost class 中的相關的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;
}
}
希望我的評論也能幫助您理解該程序! 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; }
}
如果有一種方法可以簡化 static 字符串比較的數量,或者任何人都能想到的 if 語句,那就太棒了!
根據您的描述,您希望使用 efcore 將數據從 excel 移動到 sql 服務器。
我建議您可以將數據表轉換為列表。
這是一個您可以參考的代碼示例:
static void Main(string[] args)
{
OleDbConnection connection = new OleDbConnection((@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + "D:\\test.xlsx" + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1;';"));
connection.Open();
OleDbCommand command = new OleDbCommand("SELECT * FROM [" + "Sheet1" + "$]", connection);
DataTable Data = new DataTable();
OleDbDataAdapter adapter = new OleDbDataAdapter(command);
adapter.Fill(Data);
connection.Close();
for (int i = Data.Rows.Count - 1; i >= 0; i--)
{
if (Data.Rows[i][0].ToString() == String.Empty)
{
Data.Rows.RemoveAt(i);
}
}
List<Customer> list = new List<Customer>();
list = (from DataRow dr in Data.Rows
select new Customer()
{
CustomerId = Convert.ToInt32(dr["CustomerId"]),
FirstName = dr["FirstName"].ToString(),
LastName = dr["LastName"].ToString()
}).ToList();
using (var context = new MyContext())
{
foreach (Customer item in list)
{
context.Customers.Add(item);
}
context.SaveChanges();
}
}
客戶.cs:
public class Customer
{
public int CustomerId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
Mycontext.cs:
public class MyContext:DbContext
{
public DbSet<Customer> Customers { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(@"Connectionstring");
}
}
Excel 文件:
數據庫中的數據:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.