简体   繁体   English

.Net 应用程序中的异步任务和多线程与 SQL Server 集成

[英]Async Tasks and Multi-threading in .Net Applications with SQL Server integration

Problem description : I am developing a .Net application with SQL Server interaction.问题描述:我正在开发一个与 SQL Server 交互的 .Net 应用程序。 That means the end user is able to create/update/delete entries on the SQL Server side from the application.这意味着最终用户能够从应用程序创建/更新/删除 SQL Server 端的条目。

Recently, I was trying my application when I realized the UI stops for a couple seconds until the transactions between the application and the SQL Server finishes, then the UI begins to work again.最近,当我意识到 UI 停止几秒钟直到应用程序和 SQL Server 之间的事务完成时,我正在尝试我的应用程序,然后 UI 再次开始工作。 Of course, this is not a good scenario for the end-user.当然,这对最终用户来说不是一个好场景。 So I started applying multi-threading to my application, but I realized I cannot provide access to the form until the saving process finishes.所以我开始对我的应用程序应用多线程,但我意识到在保存过程完成之前我无法提供对表单的访问。

For example: a form with one TextEdit control, the end user types his name in it and another TextEdit control, the end-user types his phone number in it then he hits the save button.例如:一个带有一个 TextEdit 控件的表单,最终用户在其中键入他的姓名和另一个 TextEdit 控件,最终用户在其中键入他的电话号码,然后点击保存按钮。 The saving SQL from my application is从我的应用程序保存 SQL 是

' Variable that holds the row number of the saved username
Dim SavedID as integer

' saving the username to the users table
SavedID = SQLControl.ExecScalar(String.format("INSERT INTO TABLE1 VALUES ({0});)", _
TextEdit1.text))

' saving the user phone number to the phone numbers table
SQLControl.ExecNonQuery(String.format("INSERT INTO TABLE2 VALUES ({0}, {1});)", _
SavedID, TextEdit2.text))

this means the end-user should not have access until the saving is finished because if he had access before the executing the second SQLNonQuery , the data might mess up.这意味着最终用户在保存完成之前不应具有访问权限,因为如果他在执行第二个SQLNonQuery之前具有访问权限,则数据可能会混乱。

PS: SQLControl.ExecScalar and SQLControl.ExecNonQuery are custom defined subroutines. PS:SQLControl.ExecScalar 和 SQLControl.ExecNonQuery 是自定义定义的子程序。

PS: I am already applying the parameters to my code but this code is only for describing my problem that I need to find a solution for. PS:我已经将参数应用于我的代码,但此代码仅用于描述我需要找到解决方案的问题。

Question : what is the best practice in dealing with such scenario if I need to apply the async tasks to save the data to SQL Server?问题:如果我需要应用异步任务将数据保存到 SQL Server,处理这种情况的最佳做法是什么?

A few things:一些东西:

  1. You're orchestrating between the UI and the database to the UI and then back to the database.您在 UI 和数据库到 UI 之间进行编排,然后再回到数据库。 At least from what's evident in your code, this is not needed and is resulting in unnecessary network latency, poor performance and does not handle failure scenarios at all.至少从您的代码中可以看出,这是不需要的,并且会导致不必要的网络延迟、性能不佳并且根本无法处理故障情况。 Try batching your calls to the database eg pass in TextEdit1.Text and TextEdit2.Text to a single stored procedure or database call and return back the SavedID if needed.尝试对数据库进行批处理,例如将 TextEdit1.Text 和 TextEdit2.Text 传递给单个存储过程或数据库调用,并在需要时返回 SavedID。

    1. You're not really following any patterns for layering or stratification.您并没有真正遵循任何分层或分层模式。 Look into the repository pattern and make sure you understand SRP (single responsibility principle).查看存储库模式并确保您了解 SRP(单一职责原则)。 The UI is only meant for presentation logic, not for making database calls. UI 仅用于表示逻辑,而不用于进行数据库调用。

    2. As others have posted here, you are opening up your application to SQL injection attacks.正如其他人在此处发布的那样,您正在向 SQL 注入攻击开放您的应用程序。 This is a major security risk.这是一个重大的安全风险。

    3. You don't need multi-threading.你不需要多线程。 You could benefit from async/TPL though.不过,您可以从 async/TPL 中受益。 Look into the ADO.NET async library - ExecuteReaderAsync, ExecuteScalarAsync, ExecuteNonReaderAsync : https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlcommand.executereaderasync?view=netframework-4.7.2查看 ADO.NET 异步库 - ExecuteReaderAsync、ExecuteScalarAsync、ExecuteNonReaderAsync: https ://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlcommand.executereaderasync ? view = netframework-4.7.2

    4. Async allows you to accomplish things in parallel using a single thread. Async 允许您使用单个线程并行完成任务。 This will make your UI responsive as background SQL tasks are completing.这将使您的 UI 在后台 SQL 任务完成时具有响应性。

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

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