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:
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?