我使用 SqlBulkCopy 以编程方式将数据从 Excel 导出到 SQL Server 2005。它工作得很好,我遇到的唯一问题是它没有保留 Excel 文件中的行顺序。我没有可供排序的列,我只想按照 Excel 电子表格中显示的顺序插入记录。
我无法修改 Excel 文件,必须使用现有的内容。按任何现有列排序都会破坏顺序。
请帮忙。
附注最终将 ID 列插入到电子表格中,看起来无法在导出/导入期间保留顺序
最佳答案
我不认为行排序是由 SQL 指定或保证的,除非您使用“ORDER BY”子句。
摘自 Bill Vaughn ( http://betav.com/blog/billva/2008/08/sql_server_indexing_tips_and_t.html ) 的帖子:
Using Order By: Even when a table has a clustered index (which stores the data in physical order), SQL Server does not guarantee that rows will be returned in that (or any particular) order unless an ORDER BY clause is used.
另一个包含信息的链接:
http://sqlblogcasts.com/blogs/simons/archive/2007/08/21/What-is-the-position-of-a-row--.aspx
关于sql-server - 如何使用 SqlBulkCopy 保持行顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/189694/