2009-06-04 41 views
1

這段代碼給我適合。返回錯誤:SqlBulkCopy不能在VB.NET中工作

The given ColumnName 'COURSENAME' does not match up with any column in data source.

如果我刪除了映射,也沒有任何數據寫入到db中。幫幫我!

Dim connectionString As String = "Data Source=2UA72518QY\SQLEXPRESS;Integrated Security=True;Pooling=False;Initial Catalog='Foo_Content'" 

Dim ds As New DataSet 
Dim sourceData As New DataTable 

'Populate Dataset with chosen XML 
ds.ReadXml(Server.MapPath("xml/FOOSECTIONS.XML")) 

'Gets the tables in the DataSet 
sourceData = ds.Tables.Add 

'Add the data 
Using destinationConnection As SqlConnection = New SqlConnection(connectionString) 

    destinationConnection.Open() 

    'Start copying! 
    Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(destinationConnection) 

     bulkCopy.ColumnMappings.Add("COURSENAME", "COURSENAME") 
     bulkCopy.ColumnMappings.Add("COURSETITLE", "COURSETITLE") 
     bulkCopy.ColumnMappings.Add("COURSEDESC", "COURSEDESC") 
     bulkCopy.ColumnMappings.Add("COURSETITLE", "COURSEFACULTY") 
     bulkCopy.ColumnMappings.Add("COURSETERM", "COURSETERM") 
     bulkCopy.DestinationTableName = "RISDCourseData" 

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

     Catch ex As Exception 
      Response.Write(ex.Message) 

     Finally 
      ' Close the SqlDataReader. The SqlBulkCopy object is automatically closed 
      ' at the end of the Using block. 
      bulkCopy.Close() 
      'Response.Redirect("Default.aspx") 
     End Try 

    End Using 
End Using 

回答

1

使用ds.Tables.Add您使用的是表sourceData - 嘗試獲得,實際上得到了裝表?在C#中,ds.Tables[0](不要問我VB ...可能ds.Tables(0)

+0

太棒了!謝謝! – mmcglynn 2009-06-05 14:33:32