export csv datatable to sql

can I have the syntax to export my datable to sql from what I have below.

Private Shared Function GetDataTabletFromCSVFile(csv_file_path As String) As DataTable
            Dim csvData As New DataTable()
            '  Try
            Using csvReader As New TextFieldParser(csv_file_path)
                csvReader.SetDelimiters(New String() {","})
                csvReader.HasFieldsEnclosedInQuotes = True
                Dim colFields As String() = csvReader.ReadFields()
                For Each column As String In colFields
                    Dim datecolumn As New DataColumn(column)
                    datecolumn.AllowDBNull = True
                    csvData.Columns.Add(datecolumn)
                Next
                While Not csvReader.EndOfData
                    Dim fieldData As String() = csvReader.ReadFields()
                    'Making empty value as null
                    For i As Integer = 0 To fieldData.Length - 1
                        If fieldData(i) = "" Then
                            fieldData(i) = Nothing
                        End If
                    Next
                    csvData.Rows.Add(fieldData)
                End While
            End Using
            '  Catch ex As Exception
            '  End Try
            Return csvData
        End Function

Open in new window

LVL 9
Seven priceFull StackAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JB4375Commented:
The Basics:

Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(yoursqlconnection)

If yourdatatable.Rows.Count > 0 Then

bulkCopy.DestinationTableName = "yoursqltable"

bulkCopy.BulkCopyTimeout =  (timeoutvalue)

bulkCopy.BatchSize = 10000

bulkCopy.WriteToServer(csvData)

End If

More info about the Bulk Copy Process here:
http://msdn.microsoft.com/en-us/library/ex21zs8x(v=vs.110).aspx
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Seven priceFull StackAuthor Commented:
You think that will work out with my code below.

 GetDataTabletFromCSVFile(Server.MapPath("~/csvfile"+ ".CSV"))


        'Protected GridView1 As Global.System.Web.UI.WebControls.GridView

        Private Shared Function GetDataTabletFromCSVFile(csv_file_path As String) As DataTable
            Dim csvData As New DataTable()
            '  Try
            Using csvReader As New TextFieldParser(csv_file_path)
                csvReader.SetDelimiters(New String() {","})
                csvReader.HasFieldsEnclosedInQuotes = True
                Dim colFields As String() = csvReader.ReadFields()
                For Each column As String In colFields
                    Dim datecolumn As New DataColumn(column)
                    datecolumn.AllowDBNull = True
                    csvData.Columns.Add(datecolumn)
                Next
                While Not csvReader.EndOfData
                    Dim fieldData As String() = csvReader.ReadFields()
                    'Making empty value as null
                    For i As Integer = 0 To fieldData.Length - 1
                        If fieldData(i) = "" Then
                            fieldData(i) = Nothing
                        End If
                    Next
                    csvData.Rows.Add(fieldData)

                    For Each TextFileTableDataRow As DataRow In csvData.Rows
                        Dim Column0 As String = IsDBNull(TextFileTableDataRow("column0"))
                        Dim Column1 As String = IsDBNull(TextFileTableDataRow("column1"))
                        Dim Column2 As String = IsDBNull(TextFileTableDataRow("column2"))
                        Dim Column3 As String = IsDBNull(TextFileTableDataRow("column3"))
                        Dim Column4 As String = IsDBNull(TextFileTableDataRow("column4"))
                        Dim Column5 As String = IsDBNull(TextFileTableDataRow("column5"))
                        Dim Column6 As String = IsDBNull(TextFileTableDataRow("column6"))

                        Dim myConnection As SqlClient.SqlConnection = New SqlClient.SqlConnection(HttpContext.Current.Application.Get("connection"))


                        Using command As New SqlCommand("INSERT INTO dbo.Table ([column0], [column1], [column2], [column3], [column4], [column5], [column6]) VALUES (@column0, @column1, @column2, @column3, @column4, @column5, @column6)", myConnection)
                            command.Parameters.AddWithValue("@column0", Column0)
                            command.Parameters.AddWithValue("@column1", Column1)
                            command.Parameters.AddWithValue("@column2", Column2)
                            command.Parameters.AddWithValue("@column3", Column3)
                            command.Parameters.AddWithValue("@column4", Column4)
                            command.Parameters.AddWithValue("@column5", Column5)
                            command.Parameters.AddWithValue("@column6", Column6)
                            '  command.Parameters.AddWithValue("@Column8", Column7)

                            myConnection.Open()
                            command.ExecuteNonQuery()

                        End Using
                    Next
                End While

            End Using
            '  Catch ex As Exception
            '  End Try
            Return csvData
        End Function

Open in new window

0
Seven priceFull StackAuthor Commented:
Ok I am using it but I am getting
The ConnectionString property has not been initialized.

  Dim myConnection As SqlClient.SqlConnection = New SqlClient.SqlConnection(HttpContext.Current.Application.Get("connection"))

Open in new window


  Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(myConnection)
                        myConnection.Open()
                        If csvData.Rows.Count > 0 Then
                            bulkCopy.DestinationTableName = "dbo.table"
                            '   bulkCopy.BulkCopyTimeout = ("")
                            bulkCopy.BatchSize = 10000
                            bulkCopy.WriteToServer(csvData)

                        End If

                    End Using
                    myConnection.Close()

Open in new window

0
JB4375Commented:
Sorry for the delay..... Basically it's telling you that it's looking for the connection string to the SQL table that you're going to send your data to.

Here's a sample code of where I was inserting into SQL table.  Your connection statement is the same, I just always preferred more of a breakdown so I could see what was going on.

Notice the connection string is "MyConnection" and then I define that connection within the web.config along with the credentials to connect with it.

(VB.NET)
Dim sConn As Data.SqlClient.SqlConnection
Dim sComm As Data.SqlClient.SqlCommand
Dim sAdapt As Data.SqlClient.SqlDataAdapter

sConn = New Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("MyConnection").ConnectionString)

sComm = New Data.SqlClient.SqlCommand("INSERT INTO YourTable(Fieldname1, Fieldname2, Fieldname3) Values ('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "')", sConn)

sAdapt = New Data.SqlClient.SqlDataAdapter(sComm)

(Web.Config File - Defines "My Connection")
<connectionStrings>
<clear/>
<add name="MyConnection" connectionString="server=YourSrvr;database=YourDB;UID=UserID;PWD=Pass" providerName="System.Data.SqlClient"/>
</connectionStrings>
0
Seven priceFull StackAuthor Commented:
thanks with some modifications.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.