更新同一个数据库时是否有必要使用多线程概念?

发布于 2024-11-26 21:51:45 字数 138 浏览 0 评论 0原文

我创建了一个 Windows 应用程序(使用 c# .net)用于我们部门的调试竞赛。 在这个过程中,许多用户使用相同的数据库来选择问题列表并单独更新其各自 ID 中的标记。 当他们更新数据库中的标记时是否需要使用线程概念.. 任何1请帮助我.. 提前谢谢...

i have created a windows application(using c# .net) for debugging contest in our department.
in this many user use the same database to select the list of questions and update the marks in their respective id alone.
does it required to use threading concept when they update their marks in the database..
any1 please help me..
thanks in advace...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

☆獨立☆ 2024-12-03 21:51:45

多线程或多线程用于您想要一次执行多个任务或同时执行某些任务的场景。您应该考虑您的场景以及在您的场景中可能使用的多线程。如果您认为某些任务可以分为两个单独的任务并且它们可以并行运行,则可以使用多线程来提高性能。同样,如果您认为某些任务很繁重并且需要花费大量时间,您可以将该任务移至后台线程并使用主线程并行处理其他一些任务。这完全取决于您的场景。

现在回到您的场景,如果它是一个 Windows 窗体应用程序,很可能一次只有一个该应用程序的用户通过 UI 进行交互。如果这个假设是正确的,我认为你不需要多线程。如果用户正在通过 UI 进行一些输入,并且他最后单击“保存”按钮将信息保存在数据库中,则不需要多线程,单个 UI 线程就足以完成此操作

Mutil-Threading or multiple threads are used in scenarios where you want to do more than one task at one time or do some tasks simultaneously. You should think about your scenario and possible use of multiple threads in your scenario. If you think there is some task which can be divided in to two separate tasks and they can run in parallel, you can use multi-threading to gain performance improvements. Similarly if you think there is some task that is heavy and takes huge time you can move that task to Background Thread and use main thread to deal with some other task in parallel. It all depends on your scenario.

Now coming to your scenario if it is a windows forms application most likely there will be only one user of this app at one time who will be interacting through UI. If this assumption is correct i don't think so you will need multi-threading. If user is doing some inputs thorough UI and he clicks save button at the end to save info in DB you don't need multi-threading a single UI thread will be enough to do this

伊面 2024-12-03 21:51:45

不,这不是必需的。每个用户都会使用数据库连接池中的一个连接,并且这些连接是并发工作的,不需要并行编程。

No this is not needed. Each user will cosume a connection from the database connection pool, and those work concurrently and no parallel programming is required.

鸩远一方 2024-12-03 21:51:45

如果您从不同的线程更新数据库,它不会损坏。这与常规 C# 不同,在常规 C# 中,您需要应用锁来保护对象。您可能需要使用事务来确保数据库更新不会在更高级别上相互干扰。非常简单地说,如果您在多个表中编辑数据库,或者如果您的数据库更改取决于数据库内容(例如添加客户的订单),则事务可以确保您的数据库保持一致,事务会阻止您为已删除的客户添加订单。

If you update a database from different threads, it will not corrupt. This is different from regular C#, where you need to apply locks to protect your objects. You may be required to use transactions to ensure that your database updates don't interfere with each other at a higher level. Very simply put, transactions ensure that your database stays consistent if you edit your database at multiple tables, or if your database changes depend on the database contents, such as adding an order from a customer, transactions prevent you add an order for a deleted customer.

甜尕妞 2024-12-03 21:51:45

您需要使用非 UI 线程进行任何数据库交互,否则 UI 可能会变得无响应。例如,如果您从 UI 线程执行长查询(或者您的连接被中断,或者数据库正在大量使用或其他任何可能出错的事情 - 都会出错),UI 线程将被阻塞,直到收到完整响应。

在有多个用户的情况下,可能会更新数据库中的相同数据,您可能需要引入事务来确保正确的控制和数据流——ACID。

You need to use non-UI thread for any database interactions, otherwise UI may become unresponsive. E.g. if you execute a long query from UI thread (or your connection was disrupted, or the database is under heavy use or whatever, anything that can go wrong - will go wrong), UI thread gets blocked until full response is received.

In the situations where you have multiple users, which may update the same data in the database, you may need to introduce transactions to ensure correct control and data flow - ACID.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文