Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 多线程和SQL查询_C#_Sql_Multithreading - Fatal编程技术网

C# 多线程和SQL查询

C# 多线程和SQL查询,c#,sql,multithreading,C#,Sql,Multithreading,有人能提供一个示例来查询多线程的sql数据库吗?比如,我有20k条DB记录,而每1000条记录都必须经过forloop。我当前的代码如下所示。 所有行都已读取到lstDevice,它有20k项 if (lstDevice.Count > 0) { for (int i = 0; i < lstDevice.Count; i += 1000) {

有人能提供一个示例来查询多线程的sql数据库吗?比如,我有20k条DB记录,而每1000条记录都必须经过forloop。我当前的代码如下所示。 所有行都已读取到lstDevice,它有20k项

       if (lstDevice.Count > 0)
            {

                for (int i = 0; i < lstDevice.Count; i += 1000)
                {
                    splitList.Add(lstDevice.Skip(i).Take(1000).ToList<DeviceHelper>());
                }

                var tasks = new Task[splitList.Count];
                int count = 0;
                foreach (List<DeviceHelper> lst in splitList)
                {
                    tasks[count] = Task.Factory.StartNew(() =>
                    {
                        QueueNotifications(lst, pMessage, pSubject,              pNotificationType,            push);
                    },
                        TaskCreationOptions.None);
                    count++;
                }
             }

现在我想修改它,使用多线程来获取1000条记录,并对其中的每一条进行处理,这基本上是发布代码的第二个foreach。

为什么要这样做?它仍然需要相同的时间,因为它将在服务器数据库上被查询。不要用并行查询垃圾邮件数据库。。。可能您首先希望查询数据库是同步的,而不是异步的,将数据库结果放入一个集合中并关闭数据库连接。然后对集合中的项执行异步多线程处理。