如何在不使用 bcp 的情况下将数据批量插入 SQL Server?
我有 SQL Server 2005 DB,其中有许多表格,
Table (id <Primary Key, Identity column>, name <VarChar (255)>)
我有一个 Java Servlet,需要能够向这些表批量添加信息。 SQL Server DB 与 Servlet 运行在不同的计算机上,并且我无权访问该计算机的文件系统。
我的理解是,我不能使用批量插入,因为它要求数据文件位于服务器的文件系统上(我做不到),它要求你填写所有字段(做不到,一个字段是由数据库填写的身份字段)或将未填写的字段放在最后一个(我将身份/id字段放在第一位,并且不想更改它),并且要求格式文件位于服务器上文件系统。
在 MySQL 中,我只需使用 Load Data Local InFile。 SQL Server 2005 似乎缺乏这样的功能。我只是错过了什么吗?有没有办法让我一次向 SQL Server 表添加多条记录而不使用 bcp 或批量插入?
格雷格·
蒂亚
I have SQL Server 2005 DB with a number of tables of the form
Table (id <Primary Key, Identity column>, name <VarChar (255)>)
I have a Java Servlet that needs to be able to bulk add information to those tables. The SQL Server DB is running on a different machine than the Servlet, and I do not have access to the file system of that machine.
My understanding is that I can't use Bulk Insert because it requires the data file to be on the server's file system (which I can't do), it requires you to fill in all fields (can't do, one field is an identity field that is filled in by the DB) or to have the unfilled field be the last one (I put the identity / id field first, and have no desire to change that), and requires the format file to be on the Server's file system.
In MySQL, I'd simply use Load Data Local InFile. SQL Server 2005 appears to lack such a capability. Am I just missing something? Is there any way for me to add more than one record at a time to a SQL Server table w/o using bcp or Bulk Insert?
TIA,
Greg
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请注意,这对于 SQL Server 2008
http:// /msdn.microsoft.com/en-us/library/ms174335%28v=SQL.100%29.aspx
对于 SQL Server 2005 及更低版本,您需要执行类似以下操作
Note this is possible with SQL Server 2008
http://msdn.microsoft.com/en-us/library/ms174335%28v=SQL.100%29.aspx
For SQL Server 2005 and Under you would need to do something like this
那么您既无法访问 servlet 计算机上的文件系统,也无法访问 SQL Server? (我也不知道如何在 MySQL 中执行此操作,因为 LOAD DATA LOCAL INFILE 需要一个文件)。
ADO.NET 支持 SqlBulkCopy API:http://msdn .microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopy.aspx
So you have neither access to the filesystem on the servlet machine nor the SQL Server? (I'm not sure how you would do this in MySQL either, since LOAD DATA LOCAL INFILE needs a file).
ADO.NET supports a SqlBulkCopy API: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopy.aspx