locked
SQLBulkCopy with strongly typed dataset RRS feed

  • Question

  • Hi,

    I am new to vb.net and I am trying to sqlbulkcopy. I have a dataset defined (.xsd). It seems that I cannot use it with sqlbulkcopy. All the information I can find on sqlbulkcopy shows that it can only take a connection string or a variable that contains the connection string.

    Is this true?

    If not, could you post some code on how I would supply the correct information to sqlbulkcopy?

    Thanks in advanced for your help!

    Monday, June 14, 2010 10:01 PM

Answers

  • You don't make a connection to your DataSet. You use a populated DataTable from your DataSet (Tables collection) as the source when you execute the WriteToServer method for SQLBulkCopy. Look at the "Using bulkCopy" block of code.

    Paul ~~~~ Microsoft MVP (Visual Basic)
    • Marked as answer by lunarpc Tuesday, June 15, 2010 5:09 PM
    • Unmarked as answer by lunarpc Tuesday, June 15, 2010 5:10 PM
    • Marked as answer by lunarpc Tuesday, June 15, 2010 5:11 PM
    Tuesday, June 15, 2010 4:13 PM

All replies

  • What is required is a DataTable or DataReader. A connection string isn't really relevant. The below example uses a DataTable:

    http://msdn.microsoft.com/en-us/library/ex21zs8x.aspx

     


    Paul ~~~~ Microsoft MVP (Visual Basic)
    Tuesday, June 15, 2010 1:42 PM
  • Hi Paul,

    I took a look at the example and I am not really seeing that SqlBulkCopy is using a DataTable, it appears that it really is using a string that stores the connection info.

    Using

     

    connection As SqlConnection = _
    New SqlConnection(connectionString)

    The function connectionString is literally the connection string to the server then

    Using

     

    bulkCopy As SqlBulkCopy = _
    New SqlBulkCopy(connection)

    connection is the above, first using statement. How do I make the connection be my strongly typed dataset?

    Tuesday, June 15, 2010 3:49 PM
  • You don't make a connection to your DataSet. You use a populated DataTable from your DataSet (Tables collection) as the source when you execute the WriteToServer method for SQLBulkCopy. Look at the "Using bulkCopy" block of code.

    Paul ~~~~ Microsoft MVP (Visual Basic)
    • Marked as answer by lunarpc Tuesday, June 15, 2010 5:09 PM
    • Unmarked as answer by lunarpc Tuesday, June 15, 2010 5:10 PM
    • Marked as answer by lunarpc Tuesday, June 15, 2010 5:11 PM
    Tuesday, June 15, 2010 4:13 PM
  • Yup, that was the conclusion I finally came to...the destination has to be defined as a connection string but you can use a dataset for the source. I was able to follow the example in the link and have is successfully update.

    Thanks for your help!

    Tuesday, June 15, 2010 5:15 PM