I have a form that needs to pull a CSV file into a datagridview, which I have working OK with the code below.
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:
Private Sub FillDataGridviewWithCSV() Dim strDirectory = Path.GetDirectoryName(ofdCSV.FileName) Dim conString As String = String.Format(ProviderString, strDirectory) ' I extracted the provider string to a separate ' sub to make this post a little easier to read ' on the forum. The string looks like this: ' "Provider=Microsoft.Jet.OLEDB.4.0; _ ' Data Source={0};Extended Properties=Text;"" Using con As New OleDbConnection(conString) con.Open() Dim cmd As New OleDbCommand(SQLString(ofdCSV.FileName), con) Using da As New OleDbDataAdapter() With {.SelectCommand = cmd} Using ds As New DataSet() da.Fill(ds, "Text") DataGridView1.DataSource = ds.Tables(0).DefaultView End Using End Using con.Close() End Using 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.VB Code:
Private Sub SQLBulkInsert(ByVal dataReader As SqlDataReader) Dim sqlCon As String = My.Settings.MyCon Using bc As New SqlBulkCopy(sqlCon) _ With {.DestinationTableName = "VendorInvoices"} bc.WriteToServer(dataReader) End Using End Sub
Can somebody point me in the right direction?




Reply With Quote