简体   繁体   English

以最有效的方式将数据库转换为 C#

[英]Database to C# in the most efficient way

在此处输入图片说明

I'm working with the above database design in SQLite.我正在 SQLite 中使用上述数据库设计。 The goal is to load the data into a C# application in the shortest amount of time possible.目标是在尽可能短的时间内将数据加载到 C# 应用程序中。 The amount of records in the Address table can go from a couple of 100's to a couple of 10.000's depending on how many Customers there are (not yet integrated in the model). Address 表中的记录数量可以从几百到几万不等,具体取决于有多少客户(尚未集成到模型中)。 Ultimately, every address in the world should be able to be in the database generating millions of records.最终,世界上的每个地址都应该能够在数据库中生成数百万条记录。 This should also be taken in consideration when inserting the data in C#.在 C# 中插入数据时也应考虑到这一点。 I know it can't be as fast if there are only 100 records, but it should just be as fast as possible.我知道如果只有 100 条记录,它不会那么快,但它应该尽可能快。

For the middle line (Continent -> Address) I came up with the following query:对于中间线(Continent -> Address),我想出了以下查询:

SELECT * FROM ((((Continent 
LEFT OUTER JOIN Country ON Continent.Code = Country.ContinentCode) 
LEFT OUTER JOIN SubDivision ON Country.Code = SubDivision.CountryCode)
LEFT OUTER JOIN City ON SubDivision.SubDivisionID = City.SubDivisionID)
LEFT OUTER JOIN Address ON City.CityID = Address.CityID)
ORDER BY Continent.Code ASC, Country.Code ASC, SubDivision.SubDivisionID ASC, City.CityID ASC;

Approach 1方法一

This query gets all the one-to-many information from the database in a single table.此查询从单个表中的数据库中获取所有一对多信息。 The way I would approach inserting this in C# is by inserting the 1st row as new data.我将在 C# 中插入它的方法是将第一行作为新数据插入。 Then, check every time if the last inserted info is different.然后,每次检查最后插入的信息是否不同。 If it's different, create a new object, if it's the same, use the last object and this for each table.如果不同,则创建一个新对象,如果相同,则对每个表使用最后一个对象和 this。 (Each table represents a model in C#.) The 5 tables would be “converted” to 5 models in a single step. (每个表代表 C# 中的一个模型。)这 5 个表将在一个步骤中“转换”为 5 个模型。

All good but then there is no information about the spoken languages, used currencies and capitals.一切都很好,但没有关于口语、使用的货币和首都的信息。 This would be added later on then by looking up indexes in the lists of continents, countries, ...这将在稍后通过在大陆、国家、...的列表中查找索引来添加。

Approach 2方法二

A second approach I'm thinking about is as follow: Query the continents and insert them in a list.我正在考虑的第二种方法如下:查询大陆并将它们插入列表中。 Query the languages and currencies and insert them in a list.查询语言和货币并将它们插入到列表中。 Query the countries with information about the languages and currencies.使用有关语言和货币的信息查询国家/地区。 This will generate duplicate country records but if sorted, can be sealed with by checking if they're already added or not and just add the language and currency to the list inside the country model.这将生成重复的国家/地区记录,但如果已排序,则可以通过检查它们是否已添加来进行密封,只需将语言和货币添加到国家/地区模型内的列表中即可。 Rinse and repeat for the rest of the data.冲洗并重复其余数据。

By doing this, each time something is inserted to C#, an index will have to be looked up in the lists so no duplicate objects exist.通过这样做,每次向 C# 插入某些内容时,都必须在列表中查找索引,以便不存在重复的对象。 Also multiple queries have to be processed to insert everything.还必须处理多个查询以插入所有内容。

Are any of my 2 approaches good or is there a 3rd better approach?我的两种方法中的任何一种是好的还是有第三种更好的方法? I will admit I haven't tested either of the above approaches to save programming time and I rather think first about the problem instead of trying something and realizing it's not working.我承认我没有测试过上述任何一种方法来节省编程时间,我宁愿首先考虑问题,而不是尝试一些东西并意识到它不起作用。

You can just load every table into dictionary with PK as a key.您可以将每个表加载到字典中,以 PK 作为键。 It would not waste memory or connection bandwidth, would be quite fast, object relations can be looked up easily.它不会浪费内存或连接带宽,速度非常快,可以轻松查找对象关系。 Question really is, why would You want to mirror the whole database into Your application.真正的问题是,您为什么要将整个数据库镜像到您的应用程序中。

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

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