简体   繁体   English

对数据库的异步操作

[英]Asynchronous operations on database

I am building a WF application "Newsletter" to send emails to selected Email Groups which are created from this application and stored in a .sdf database which is SQL Server Compact Edition, built into Visual Studio 2012. 我正在构建WF应用程序“ Newsletter”,以将电子邮件发送到从该应用程序创建的选定电子邮件组中,并存储在Visual Studio 2012中内置的SQL Server Compact Edition的.sdf数据库中。

As of now I did this application running on Main thread, but this is not the approach that should be used with databases, I would like to do all operations on the database asynchronously. 到目前为止,我已经在主线程上运行了该应用程序,但是这不是应该与数据库一起使用的方法,我想异步地对数据库执行所有操作。 I have read about few approaches, but I never used them and am puzzled. 我已经读过几种方法,但是我从未使用过,并且感到困惑。

For ppl who are interested how DB and relations look like read square brackets beneath: 对于感兴趣的数据库人,请阅读下面的方括号,以了解数据库和关系的外观:

As of now there are 2 tables: 截至目前,有2个表:

  1. Emails (int UniqueID, string Email) Emails (int UniqueID,字符串电子邮件)
  2. Groups (int ID, string Grupa) Groups (int ID,字符串Grupa)

PK in 1st table is UniqueID+Email and in 2nd ID+string , in 2nd table FK to table 1 is ID and it connects to UniqueID . 第一个表中的PK是UniqueID+Email ,第二个ID+string PK ID+string ,表1的第二个表FK中的PK是ID ,它连接到UniqueID

I am going to add 3rd table with ExistingGroups with 1 column ExstGroup and it will be related to table 2, where Grupa will be FK to ExistingGroups and this 3rd table will store names of all groups in our database and will be used to dynamically create checkboxes (stored in checkedListBox?) when application starts it will be triggerd in FormLoad event I suppose, correct me if I'm wrong 我将添加带有第1列ExstGroup ExistingGroups第3个表,它将与表2相关,其中Grupa将FK ExstGroupExistingGroups ,此第3个表将在我们的数据库中存储所有组的名称,并将用于动态创建复选框(存储在checkedListBox中?)应用程序启动时,它将在我想的FormLoad事件中触发,如果我输入错误,请更正我

I would like to use ADO.net connected layer approach, so here are a few questions: 我想使用ADO.net连接层方法,所以这里有几个问题:

  1. Where should i establish connection to database (conn.Open()), during Form_Load event? 在Form_Load事件期间,我应该在哪里建立与数据库(conn.Open())的连接? And should I check if this conn is open b4 attempting to execute any operation on DB? 并且我应该检查conn是否已打开b4试图在DB上执行任何操作?

  2. Here is code snippet of taking Emails from DB and inserting it into hashset to pass those emails to SendingEmail function. 这是从数据库获取电子邮件并将其插入哈希集以将那些电子邮件传递给SendingEmail函数的代码片段。 How would you change it into asynchronous? 您如何将其更改为异步?

      // CheckedEmails is Form variable to store emails from groups which where checked on checkboxes. HashSet<string> CheckedEmails = new HashSet<string>(); private HashSet<string> ListOfEmails() { CheckedEmails.Clear(); Queue<string> checkBoxes = new Queue<string>(); if (IT_checkbox.Checked) //checking all checkboxes this way.. { checkBoxes.Enqueue(IT_checkbox.Text); } SqlCeCommand cmd = conn.CreateCommand(); while (checkBoxes.Count() != 0) { cmd.CommandText = string.Format("Select Email FROM Emails INNER JOIN Groups ON Emails.UniqueID=Groups.ID WHERE Groups.Grupa='{0}' ", checkBoxes.Dequeue()); SqlCeDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { CheckedEmails.Add((string)dr["Email"]);//adding emails fromcheckedgroups } dr.Close(); } return CheckedEmails; } 

2A) Lets assume that this operation on DB is costly and takes some time, how would I prevent user from performing, this operation on DB many times, or other operations on DB (like adding new Emails) while this operation is in progress? 2A)假设对该数据库进行的操作成本高昂并且花费一些时间,我将如何阻止用户多次执行该操作,对该数据库进行该操作,或者在进行该操作时如何对数据库进行其他操作(例如添加新的电子邮件)?

3.Can I use something to let user know that operation on DB is currently running, like some progress bar or changing mouse cursor to Loading? 3.我是否可以使用某种方式让用户知道数据库上的操作正在运行,例如某些进度条或将鼠标光标更改为Loading? And How? 如何?

Thank you all for your help in advance, I tried to be as precise as I could, but if anything is not understandable please ask. 谢谢大家的帮助,我尽我所能,但如果无法理解,请询问。

2)您可以使用异步和等待功能;)

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

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