Results 1 to 3 of 3

Thread: Push Dataset into SQL Server

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Push Dataset into SQL Server

    I have a Class that is creating a large dataset (over 32,000 records) on the fly. I want to take the dataset and import it directly into SQL Server 2005 but I have not been able to find anything in ADO that allows me to do that all in one chunk.

    Doing a search I found some people in a similar situation that have exported the dataset to an XML file and then imported into SQL Server, which seems like a long way to go just to get something done that I would think would be part of ADO.Net.

    However, for lack of a better option, I went a head and created the XML file but now I am having problems getting it to import into SQL Server so before I lose another day of productivity I am goin to ask a question.

    Is there a way to insert a dataset directly into SQL Server without going to a file first (even if I have to do it one row at a time)?
    VB Code:
    1. '        cnn.Open()
    2.         Try
    3.             Dim cmd As SqlCommand = New SqlCommand("TimeCards", cnn)
    4.             cmd.CommandType = CommandType.StoredProcedure
    5.             cmd.CommandTimeout = 0
    6.             cmd.Parameters.AddWithValue("@StateID", "45")
    7.             cmd.Parameters.AddWithValue("@StoreID", "ABCDEF")
    8.             cmd.Parameters.AddWithValue("@From", dtStartDate)
    9.             cmd.Parameters.AddWithValue("@To", dtEndDate)
    10.             cmd.ExecuteNonQuery()
    11.             da.SelectCommand = cmd
    12.             da.Fill(dsHours, "EmpHours")
    13.  
    14.             CheckForFile(strFilePath)
    15.  
    16.         Catch ex As Exception
    17.             bolSucceed = False
    18.         Finally
    19.             cnn.Close()
    20.             cnn.Dispose()
    21.             da.Dispose()
    22.         End Try

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Push Dataset into SQL Server

    Yes, you can call the Update method of your SqlDataAdapter and pass in the dataset. Having said that, the Update method will do all 3 operations (insert, delete and update) depending on the rowstate of each datarow... I fyou want in insert only, make sure that all of your datarows have the rowstate = rowstate.added.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Push Dataset into SQL Server

    you could also use SMO's Bulk Copy and pass it a datatable to do a single mass import. The problem with going the table adapter route is that it will make 32,000 calls to the database (one connection, but each row gets it's own call for insert/update).... the Problem with the Bulk insert is that the layout of the datatable MUST MATCH the layout of the destination table.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width