944,116 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 32630
  • VB.NET RSS
Aug 8th, 2006
-1

Importing csv file to SQL Server Using VB.Net

Expand Post »
I have a csv file that needs to be imported into sql server using vb.net.

I got it working with the following code with one exception:
VB.NET Syntax (Toggle Plain Text)
  1. Private Function ImportLeadFile(ByVal projectfile As String, ByVal sLeadFile As String, ByVal DATABASE As String) As Boolean
  2. Dim objConn As nsSqlClient.SqlConnection
  3. Dim ds As New DataSet
  4. Dim m_strConnection As String = "server=NINEL-D246655F1;Initial Catalog=TimeControl;user id=timeuser;password=timeuser;"
  5.  
  6. objConn = New nsSqlClient.SqlConnection
  7. objConn.ConnectionString = m_strConnection
  8. objConn.Open()
  9.  
  10. ' Make sure the .CSV file exists:
  11. If File.Exists(sLeadFile) Then
  12. Try
  13. ' ------ Load the data from the .CSV file: ----------
  14. Dim strSQL As String
  15. strSQL = "Select * " & _
  16. " INTO " & DATABASE & ".dbo.[List_staging] " & _
  17. "FROM OPENROWSET('MSDASQL','Driver={Microsoft Text Driver (*.txt; *.csv)}; DEFAULTDIR=C:\VoicenetSQL\project\tampa\Politic\" & projectfile & "; Extensions=CSV; HDR=No;','SELECT * FROM at1008.csv') "
  18.  
  19. Dim objCommand As nsSqlClient.SqlCommand
  20. objCommand = New nsSqlClient.SqlCommand(strSQL, objConn)
  21.  
  22. objCommand.CommandText = strSQL
  23. objCommand.ExecuteNonQuery()
  24. objConn.Close()
  25. Catch ex As Exception
  26. sResultText = sResultText & "<BR>" & ex.Message
  27. End Try
  28. End If
  29. End Function

The csv file contains one column of phone numbers with no heading. When the file gets imported into a table the first phone number record is created as a column name. How can I get around this?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ninelg is offline Offline
6 posts
since Aug 2005
Aug 9th, 2006
0

Re: Importing csv file to SQL Server Using VB.Net

Hi,

I think you would need to write your own CSV parser using String.Split and not use OLEDB Text Driver (eventhough there might be a parameter for the connection string to fix this, check text driver's reference).

Loren Soth
Reputation Points: 28
Solved Threads: 4
Posting Whiz in Training
Lord Soth is offline Offline
233 posts
since Mar 2006
Aug 9th, 2006
0

Re: Importing csv file to SQL Server Using VB.Net

How do I do that?

I'd really appreciate any help.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ninelg is offline Offline
6 posts
since Aug 2005
Feb 25th, 2009
0

Re: Importing csv file to SQL Server Using VB.Net

Ninleg,

Here is a sample:

I have a Categories.txt file in C:\ that has the following contents:

"Sushi", "All kinds of sushi rolls, including crab tempura roll"
"Wine", "All kinds of wine, including some world famous Australian Shiraz"

To import it into Northwind, create the DataSet and TableAdapters.

Use the following code to import. See comments in code for explanation.

'Opening the file
Using file As New IO.FileStream("C:\Categories.txt", IO.FileMode.Open, IO.FileAccess.Read)
'Creating a strem to read the file into text
Using stream As New IO.StreamReader(file)
'Read the entire file, split it into line arrays by CRLF, and loop through it.
For Each sLine As String In stream.ReadToEnd().Split(Environment.NewLine)
'You to use ", as the delimiter to ensure that commas inside a record are ignored.
Dim sDelimiters() As String = {""","}
'Now split the line into individual records.
Dim sRecords() As String = sLine.Split(sDelimiters, StringSplitOptions.None)
'Insert them into the database by referencing the individual string in the array,
'but be sure to replace the quotes with an emtpy string; otherwise, you will have quote in the record.
'The third argument, to which I'm passing a 'Nothing', is expecting a picture.
CategoriesTableAdapter.Insert(sRecords(0).Replace("""", ""), sRecords(1).Replace("""", ""), Nothing)
Next
End Using
End Using


- Jacob Silberstein
Last edited by jjthebig1; Feb 25th, 2009 at 11:24 pm.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
jjthebig1 is offline Offline
2 posts
since Feb 2009
Aug 19th, 2011
0
Re: Importing csv file to SQL Server Using VB.Net
When using Text Driver method, "Schema.ini" will be created in the application path.
In the file, specify the "ColNameHeader=False". Then it won't consider the first row as header.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
t_karthik is offline Offline
1 posts
since Aug 2011

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Exception thrown when utilising My.Settings
Next Thread in VB.NET Forum Timeline: ComboBox Itemlist trouble





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC