English 中文(简体)
使用 SQLBulkCopy 导入数据
原标题:Importing data using SQLBulkCopy

我们有一个遗留应用程序, 将大量数据倾弃到标签分隔的文件上。 每个文件都包含一个单一的记录类型, 所有字段都有固定的长度 。

These files can readily be imported into corresponding tables in our SQL server database using the BCP utility from the command line. We have a VB.Net program written in VS 2003 that imports these files using the SQLDMO.BulkCopy routine.

我们正在更新系统,以便使用2008年SQL服务器使用VS 2010,根据微软文件SQLDMO,SQLDMO不再可用。

我搜索了互联网, 并重写了导入程序, 用 Microsoft. Jet. OLEDB. 4. 4 提供商将标签分隔的文件导入到数据表。 SqlClient. BulkCopy 对象随后被用来导入此数据表。 问题是, 被设置为空格的标签分隔文件中的字段在导入数据表时被作为 NULLs 处理。 当数据表由 SqlClient 处理时, 复制失败, 因为无效值被定义为 NUL 的 SQL 表格字段拒绝 。

正在测试的代码如下:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim data As DataTable = RetrieveSourceData()
    CopyData(data)
End Sub

Private Function RetrieveSourceData() As DataTable
    Dim connstring As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:	emp;Extended Properties= text;HDR=No;FMT=TabDelimited "
    Dim sourcedata As New DataTable
    Using conn As New OleDb.OleDbConnection(connstring)
        conn.Open()
        Dim command As New OleDb.OleDbCommand("Select * from X1.CSV", conn)
        Dim adapter As New OleDb.OleDbDataAdapter(command)
        adapter.Fill(sourcedata)
        conn.Close()
    End Using
    Return sourcedata
End Function

Private Sub CopyData(SourceData As DataTable)
     Dim dbConnString As String = "Data Source=(local);Initial Catalog=XtractDB;User ID=xxxx;Password=yyyy;"
    Using bcp As New SqlClient.SqlBulkCopy(dbConnString)
        bcp.DestinationTableName = "X1"
        bcp.BatchSize = 1000
        bcp.WriteToServer(SourceData)
    End Using
End Sub

为使输入文件被识别为 TabDelim I 必须创建与输入文件相同的目录中的 schema. ini 文件, 以便识别输入文件为 TabDelim I 。 内容显示于下

[X1.CSV]
Format=TabDelimited

在创建数据表时,我是否可以强迫有空格的字段不被视为 NULL?

这是通过 VB.Net 程序处理散装副本的最佳方法吗?

提亚,

安迪

保存到: 默认

Switch color theme
Select message background color...
Select message area width...
Adjust message text font size...
Disable auto links Enable acronyms Disable message header Enable auto quote Update the title of this thread... SQL Bulk Copy Thread #1544244 Message #1544244

问题回答

SqlDMO is deprecated in Sql server 2012 but it s available till Sqlserver 2008 R2 version. SqlBulkCopy is available in all .net frameworks till 4.5 except 1.1.

"http://msdn.microsoft.com/en-us/library/cc707782.aspx" rel="no follow">请参考此链接进行校验

您可以使用 BULK INSERT (命令行工具) 立即插入多个记录 。

BULK
INSERT Tablename
FROM  c:csvtest.txt 
WITH
(
    FIELDTERMINATOR =  , 
    ,ROWTERMINATOR =  
 
    --,FIRSTROW = 2
    --,MAXERRORS = 0
)
GO




相关问题
Is Shared ReadOnly lazyloaded?

I was wondering when I write Shared ReadOnly Variable As DataType = New DataType() Or alternatively Shared ReadOnly Variable As New DataType() Is it lazy loaded or as the instance initializes? ...

Entertaining a baby with VB.NET

I would like to write a little application in VB.NET that will detect a baby s cry. How would I get started with such an application?

Choose Enter Rather than Pressing Ok button

I have many fields in the page and the last field is a dropdown with list of values. When I select an item in a dropdown and press Enter, it doesn t do the "Ok". Instead I have to manually click on Ok ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

Set Select command in code

On button Click I want to Set the Select command of a Gridview. I do this and then databind the grid but it doesn t work. What am i doing wrong? protected void bttnView_Click(object sender, ...

Hover tooltip on specific words in rich text box?

I m trying to create something like a tooltip suddenly hoovering over the mouse pointer when specific words in the richt text box is hovered over. How can this be done?

热门标签