简体   繁体   English

批量从SQL Server检索数据

[英]Retrieving data from SQL server in batches

I have a Silverlight web application. 我有一个Silverlight Web应用程序。

I am inserting records into a table (SQL Database), loaded from a csv file. 我将记录插入到从csv文件加载的表(SQL数据库)中。 I tried loading +- 15 000 records and it throws me out with the following error: The remote server returned an error: NotFound. 我尝试加载+ - 15 000条记录,它会抛出以下错误:远程服务器返回错误:NotFound。

I gather it is because it is just too much data to insert at once because when I split it into 'batches', say 100 at a time, it inserted into the table no probs. 我收集它是因为它一次插入的数据太多了,因为当我将它分成'批次'时,一次说100,它插入表中没有probs。 Even 500 at a time was too much. 一次只有500个太多了。

What I do after I insert it into the table, is read from that same table the data and put it into a datagrid. 我将其插入表中后所做的是从同一个表中读取数据并将其放入数据网格中。 This is so the user can see that it was inserted successfully and also monitor as the records that were inserted are processed. 这样用户就可以看到它已成功插入,并且还会在处理插入的记录时进行监视。

Now obviously I am getting the same error when attempting to load the 15000 +- records back to a datagrid. 现在显然我在尝试将15000 + - 记录加载回数据网格时遇到了同样的错误。

My question is how can I read the records in the table also in batches? 我的问题是如何批量阅读表中的记录?

Hope somebody can help. 希望有人可以提供帮助。

Many thanks, 非常感谢,

Neill 奥尼尔

EDIT 编辑

To test I made a change to the OperationContract: 为了测试我对OperationContract进行了更改:

Originally 本来

[OperationContract]
public List<send_box> GetSendingItems()
{
    return (from a in smsData.send_boxes
            orderby a.sb_log descending
            select a).ToList();
}

Changed To 变成

[OperationContract]
public List<send_box> GetSendingItems()
{
    List<send_box> sendBoxList = (from a in smsData.send_boxes
            orderby a.sb_log descending
            select a).ToList();

    return sendBoxList;
}

The result are returned from the database, but when I try to return it to the application: --> return sendBoxList 结果从数据库返回,但是当我尝试将其返回给应用程序时: - > return sendBoxList

Then it throws out the "The remote server returned an error: NotFound." 然后它抛出“远程服务器返回错误:NotFound”。 error. 错误。 Hope this extra info will assist 希望这些额外信息会有所帮助

Regards 问候

Neill 奥尼尔

I'm not sure if this is your exact problem based on the limited error message you posted (stack trace would help to diagnose this better), but I am assuming that you are using a WCF service to pass your data to the database and back and the service has a maximum message size set which you would need to increase to allow for larger amounts of data to be passed. 我不确定这是否是您发布的有限错误消息的确切问题(堆栈跟踪有助于更好地诊断),但我假设您正在使用WCF服务将数据传递到数据库并返回并且该服务具有最大消息大小集,您需要增加该消息大小以允许传递更大量的数据。 Here's an article that talks about this wcf-how-to-increase-message-size-quota 这篇文章讨论了这个wcf-how-to-increase-message-size-quota

This looks like a job to perform with a bulk insert mechanism like SqlBulkCopy (work flow: transfer the csv file to the server, build up a data table or something and insert in one go). 这看起来像是使用SqlBulkCopy等批量插入机制执行的工作(工作流程:将csv文件传输到服务器,构建数据表或其他内容并一次插入)。 Using batches is a good idea anyhow, eg 1000 items at a time should not be a problem. 无论如何,使用批次是一个好主意,例如,一次1000个项目应该不是问题。

Side remark: why on earth would you want to display 15000 records in a grid? 旁注:为什么你想在网格中显示15000条记录? There is no point... Eg would it not be better to eg show the last 10 inserted records, together with a total count? 没有意义......例如,显示最后10个插入的记录以及总计数是不是更好?

I am not sure what is your exact business requirement. 我不确定您的确切业务需求是什么。 But going by one of your response, you said user wnats to see all the records. 但是按照你的一个回复,你说用户会看到所有的记录。 I am not sure what the user would be able to do if all the 15,000 records fail. 如果所有15,000条记录都失败,我不确定用户能够做什么。 May be it would be a good idea to show a kind of summary saying out of lets say 15000, 10000 passed and 5000 failed. 可能是一个好主意,显示一个总结说,让我们说15000,10000通过,5000失败。 And provide drill down or navigation link to explore the success or failure records. 并提供向下钻取或导航链接以探索成功或失败记录。

Secondly, even though user wants to see all records, I don't think its user friendly to display more than 100 records in a grid. 其次,即使用户想要查看所有记录,我也认为用户不会在网格中显示超过100条记录。 100 is also extreame for me. 100对我来说也是极端的。 You can implement paging functionality to limit the number of records displayed in the grid. 您可以实现分页功能以限制网格中显示的记录数。

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

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