首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SQLBulkCopy不工作

SQLBulkCopy不工作
EN

Stack Overflow用户
提问于 2010-11-30 11:23:22
回答 1查看 1K关注 0票数 1

这是我第一次尝试使用sqlbulkcopy类。当我运行它时,什么也没有发生,也没有任何错误。请帮帮我。

以下是我的代码:

代码语言:javascript
复制
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration.ConfigurationManager

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim connectionString As String = GetConnectionString()
    ' Open a connection 
    Using sourceConnection As SqlConnection = New SqlConnection(connectionString)
        sourceConnection.Open()

        ' Perform an initial count on the destination table.
        Dim commandRowCount As New SqlCommand("SELECT COUNT(*) FROM dbo.BRANCH;", sourceConnection)
        Dim countStart As Long = System.Convert.ToInt32(commandRowCount.ExecuteScalar())
        Console.WriteLine("Starting row count = {0}", countStart)

        ' Get data from the source table as a SqlDataReader.
        Dim commandSourceData As SqlCommand = New SqlCommand("select * from BRANCH", sourceConnection)
        Dim reader As SqlDataReader = commandSourceData.ExecuteReader
        UpdateHQDB(reader)

    End Using
End Sub

Private Function GetConnectionString() As String
    Return "Data Source=127.0.0.1;Initial Catalog=SOURCEDB;User ID=sa;Password="
End Function

Private Function GetDestString() As String
    Return "Data Source=192.168.123.194;Initial Catalog=DESTINATIONDB;User ID=sa;Password="
End Function

Public Sub UpdateHQDB(ByVal reader)
    Dim DestConString As String = GetDestString()
    ' Open a connection 
    Using DestinationConnection As SqlConnection = New SqlConnection(DestConString)
        DestinationConnection.Open()

        ' Perform an initial count on the destination table.
        Dim DestcommandRowCount As New SqlCommand("SELECT COUNT(*) FROM dbo.BRANCH;", DestinationConnection)
        Dim DestcountStart As Long = System.Convert.ToInt32(DestcommandRowCount.ExecuteScalar())
        Console.WriteLine("Starting row count at destination = {0}", DestcountStart)

        Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(DestinationConnection)
            bulkCopy.DestinationTableName = "dbo.BRANCH"

            Try
                ' Write from the source to the destination.
                bulkCopy.WriteToServer(reader)

            Catch ex As Exception
                Console.WriteLine(ex.Message)

            Finally
                ' Close the SqlDataReader. The SqlBulkCopy
                ' object is automatically closed at the end
                ' of the Using block.
                reader.Close()
            End Try
        End Using

        ' Perform a final count on the destination table
        ' to see how many rows were added.
        Dim countEnd As Long = _
            System.Convert.ToInt32(DestcommandRowCount.ExecuteScalar())
        Console.WriteLine("Ending row count = {0}", countEnd)
        Console.WriteLine("{0} rows were added.", countEnd - DestcountStart)

        Console.WriteLine("Press Enter to finish.")
        Console.ReadLine()
    End Using
End Sub
End Class
EN

回答 1

Stack Overflow用户

发布于 2010-11-30 11:33:28

我认为应该使用SqlBulkCopy的NotifyAfter和SqlRowsCopied事件来排除故障

下面是c#中的代码

代码语言:javascript
复制
        sb.NotifyAfter = 1;
        sb.SqlRowsCopied += new SqlRowsCopiedEventHandler(sb_SqlRowsCopied);



    void sb_SqlRowsCopied(object sender, SqlRowsCopiedEventArgs e)
    {
       // See if this event fired
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4310247

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档