| | |
Importing csv file to SQL Server Using VB.Net
Please support our VB.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2005
Posts: 6
Reputation:
Solved Threads: 0
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:
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?
I got it working with the following code with one exception:
VB.NET Syntax (Toggle Plain Text)
Private Function ImportLeadFile(ByVal projectfile As String, ByVal sLeadFile As String, ByVal DATABASE As String) As Boolean Dim objConn As nsSqlClient.SqlConnection Dim ds As New DataSet Dim m_strConnection As String = "server=NINEL-D246655F1;Initial Catalog=TimeControl;user id=timeuser;password=timeuser;" objConn = New nsSqlClient.SqlConnection objConn.ConnectionString = m_strConnection objConn.Open() ' Make sure the .CSV file exists: If File.Exists(sLeadFile) Then Try ' ------ Load the data from the .CSV file: ---------- Dim strSQL As String strSQL = "Select * " & _ " INTO " & DATABASE & ".dbo.[List_staging] " & _ "FROM OPENROWSET('MSDASQL','Driver={Microsoft Text Driver (*.txt; *.csv)}; DEFAULTDIR=C:\VoicenetSQL\project\tampa\Politic\" & projectfile & "; Extensions=CSV; HDR=No;','SELECT * FROM at1008.csv') " Dim objCommand As nsSqlClient.SqlCommand objCommand = New nsSqlClient.SqlCommand(strSQL, objConn) objCommand.CommandText = strSQL objCommand.ExecuteNonQuery() objConn.Close() Catch ex As Exception sResultText = sResultText & "<BR>" & ex.Message End Try End If 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?
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
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
Best regards,
Loren Soth
Crimson K. Software _________________________________________________________________ Crimson K. Blog
Loren Soth
Crimson K. Software _________________________________________________________________ Crimson K. Blog
•
•
Join Date: Feb 2009
Posts: 2
Reputation:
Solved Threads: 1
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
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.
![]() |
Similar Threads
- How to connect queries from SQL Server 2005 to VB.Net Windows application (VB.NET)
- How to connect queries from SQL Server 2005 to VB.Net Windows application (MS SQL)
- Importing excel data to sql server (C#)
- Loops in Sql Server Triggers and Stored Procedures (ASP.NET)
- Close all active mssql server connections from vb.net (VB.NET)
- Reading CSV file into a ADO recordset (ASP.NET)
- Simple ASP.Net Login Page (Using VB.Net) (ASP.NET)
Other Threads in the VB.NET Forum
- Previous Thread: "Flush()" Serialized object over a Networkstream using the BinaryFormatter
- Next Thread: DataGridView and combobox
Views: 14687 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for VB.NET
.net 2005 2008 access account application arithmetic array arrays basic bing button buttons c# center check checkbox code combobox component convert crystalreport data database databasesearch datagrid datagridview design designer dissertation dissertations dropdownlist excel file-dialog folder ftp generatetags google gridview hardcopy highlighting images inline insert installer intel internet listview mobile monitor ms net networking output passingparameters peertopeervideostreaming picturebox picturebox1 plugin port print printing problem problemwithinstallation project save searchbox searchvb.net select serial server soap sorting studio syntax table tcp text textbox time timer toolbox trim update updown user vb vb.net vb.netcode vb.netformclosing()eventpictureboxmessagebox vb2008 vbnet view visual visualbasic visualbasic.net visualstudio visualstudio2008 web wpf





