Solved

export csv datatable to sql

Posted on 2014-02-26
5
502 Views
Last Modified: 2014-02-26
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

0
Comment
Question by:Seven price
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 1

Accepted Solution

by:
JB4375 earned 500 total points
ID: 39890239
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
 
LVL 9

Author Comment

by:Seven price
ID: 39890290
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
 
LVL 9

Author Comment

by:Seven price
ID: 39890460
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
 
LVL 1

Expert Comment

by:JB4375
ID: 39890898
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
 
LVL 9

Author Closing Comment

by:Seven price
ID: 39890928
thanks with some modifications.
0

Featured Post

Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question