简体   繁体   English

实现一种执行一些繁重操作(例如更新数据库,从数据库检索)的方法的最佳方法是什么?

[英]What is the best way to implement a method that does some pretty heavy operations like updating a database, retrieving from a database?

I have a piece of code about few hundread lines, this code is inside a while loop which gets executed each time for about 1000 records. 我有一段关于几行hundread行的代码,该代码位于while循环内,每次循环执行约1000条记录。

I want to move this code out of the while loop and place it in a seperate method , what is the best way improve the performance of the application 我想将这段代码移出while循环,然后将其放在单独的方法中,提高应用程序性能的最佳方法是什么?

Implementing the code as a static method, or a non static method 将代码实现为静态方法还是非静态方法

Note: The code contains objects like Datasets, Datareaders, and data adapters whic 注意:该代码包含诸如数据集,数据读取器和数据适配器之类的对象

There is almost no difference between calling a static method and calling an instance method. 调用静态方法和调用实例方法几乎没有区别。 There is a slight difference since one is doing CLR call and the other doing virtual call and CLR has to navigate the types to find the implementation but that is really negligible . 两者之间存在细微的差异,因为一个正在执行CLR call ,而另一个正在进行virtual call而CLR必须导航类型以查找实现,但这确实可以忽略不计

Your main concern here must be readability when you have a 1000-line code rather than performance. 当您拥有1000行代码而不是性能时,此处的主要问题必须是可读性。 Whatever you do, calling your database will be the slowest part of your code so instead focus on refactoring . 无论您做什么,调用数据库都是代码中最慢的部分,因此请专注于重构

静态或非静态对您的情况没有影响。

I would move the code into the DoWork method of a BackgroundWorker . 我将代码移到BackgroundWorkerDoWork方法中。 This way, your code can run in a background thread, give periodic progress updates to the main UI thread, and let the UI thread know when the operations are completed by invoking the WorkerCompleted event. 这样,您的代码可以在后台线程中运行,对主UI线程进行定期进度更新,并通过调用WorkerCompleted事件让UI线程知道何时完成操作。

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

相关问题 实施重新排序方法的最佳方法是什么? - What is the best way to implement a reordering method? 从桌面应用程序操作MySQL数据库的最佳方法是什么? - What is the best method of manipulate a MySQL database from a desktop application? 从数据库将数据加载到以下结构的最佳方法是什么? - What is the best way to load data to the following structure from database? 从数据库中的数据生成XML的最佳方法是什么? - What is the best way to generate XML from the data in the database? 使用 DataGrid 从数据库中删除记录的最佳方法是什么 - What is the best way to delete records from database using DataGrid 从 C# 连接和使用 sqlite 数据库的最佳方法是什么? - What is the best way to connect and use a sqlite database from C# WPF-将数据从数据库绑定到复选框控件的最佳方法是什么 - WPF - what is the best way to bind data from a database to a checkbox control 从c#批量数据库插入的最佳方法是什么? - What’s the best way to bulk database inserts from c#? 从数据库检索数据的高级方法 - Advanced method for retrieving data from the database 在数据库中存储货币价值的最佳方式是什么? - What is the best way to store a money value in the database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM