Results 1 to 4 of 4

Thread: (Resolved) CSV > Datagridview > SQL Server 2008

Threaded View

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2010
    Posts
    28

    (Resolved) CSV > Datagridview > SQL Server 2008

    I have a form that needs to pull a CSV file into a datagridview, which I have working OK with the code below.
    VB Code:
    1. Private Sub FillDataGridviewWithCSV()
    2.         Dim strDirectory = Path.GetDirectoryName(ofdCSV.FileName)
    3.         Dim conString As String = String.Format(ProviderString, strDirectory)
    4.         ' I extracted the provider string to a separate
    5.         ' sub to make this post a little easier to read
    6.         ' on the forum. The string looks like this:
    7.         ' "Provider=Microsoft.Jet.OLEDB.4.0; _
    8.         ' Data Source={0};Extended Properties=Text;""
    9.  
    10.         Using con As New OleDbConnection(conString)
    11.             con.Open()
    12.             Dim cmd As New OleDbCommand(SQLString(ofdCSV.FileName), con)
    13.             Using da As New OleDbDataAdapter() With {.SelectCommand = cmd}
    14.                 Using ds As New DataSet()
    15.                     da.Fill(ds, "Text")
    16.                     DataGridView1.DataSource = ds.Tables(0).DefaultView
    17.                 End Using
    18.             End Using
    19.             con.Close()
    20.         End Using
    21.     End Sub
    Now that I have it in a datagridview I need allow the user to press a button and push it into SQL server. I know it can be done with this but I am not sure how to take what I have in the DataGridview, the DataAdapter or the DataSet into a DataReader.
    VB Code:
    1. Private Sub SQLBulkInsert(ByVal dataReader As SqlDataReader)
    2.         Dim sqlCon As String = My.Settings.MyCon
    3.         Using bc As New SqlBulkCopy(sqlCon) _
    4.         With {.DestinationTableName = "VendorInvoices"}
    5.             bc.WriteToServer(dataReader)
    6.         End Using
    7.     End Sub
    I have tried many different variations on converting what is in the DataGrid (or what is feeding it) into somthing I can push into the BulkCopy but have not got it to work right.

    Can somebody point me in the right direction?
    Last edited by Elfish; Jul 21st, 2010 at 11:18 AM.

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