PC Review


Reply
Thread Tools Rate Thread

Bulk insert problem when the last field in the last row is null

 
 
damezumari
Guest
Posts: n/a
 
      7th Nov 2005
I have a csv file I want to insert into an existing sql-server table
using a vb.net windows form. This is my code:

Try

Dim cn As New SqlConnection
cn.ConnectionString = _
"workstation id=""F2RS-D477"";packet size=4096;user
id=sa;data source=""F2RS-D477"";persist security info=False;initial
catalog=pubs"
cn.Open()

Dim cmd As New SqlCommand
Dim table As String = "pubs.dbo.eaccount"
Dim file As String = "C:\test2.csv"
cmd.CommandText = "BULK INSERT " & table & _
" FROM '" + file + "' WITH (FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n')"
cmd.Connection = cn
cmd.ExecuteNonQuery()

cn.Close()
cn = Nothing

Catch
MsgBox(Err.Description)
End Try

It does not work if the value of the last field in the last row is
empty. If it is " " it works.

How do I get the code to always work?

 
Reply With Quote
 
 
 
 
Paul Clement
Guest
Posts: n/a
 
      7th Nov 2005
On 7 Nov 2005 09:09:11 -0800, "damezumari" <(E-Mail Removed)> wrote:

¤ I have a csv file I want to insert into an existing sql-server table
¤ using a vb.net windows form. This is my code:
¤
¤ Try
¤
¤ Dim cn As New SqlConnection
¤ cn.ConnectionString = _
¤ "workstation id=""F2RS-D477"";packet size=4096;user
¤ id=sa;data source=""F2RS-D477"";persist security info=False;initial
¤ catalog=pubs"
¤ cn.Open()
¤
¤ Dim cmd As New SqlCommand
¤ Dim table As String = "pubs.dbo.eaccount"
¤ Dim file As String = "C:\test2.csv"
¤ cmd.CommandText = "BULK INSERT " & table & _
¤ " FROM '" + file + "' WITH (FIELDTERMINATOR = ',',
¤ ROWTERMINATOR = '\n')"
¤ cmd.Connection = cn
¤ cmd.ExecuteNonQuery()
¤
¤ cn.Close()
¤ cn = Nothing
¤
¤ Catch
¤ MsgBox(Err.Description)
¤ End Try
¤
¤ It does not work if the value of the last field in the last row is
¤ empty. If it is " " it works.
¤
¤ How do I get the code to always work?

What happens? Do you get an error? Is the column skipped?


Paul
~~~~
Microsoft MVP (Visual Basic)
 
Reply With Quote
 
damezumari
Guest
Posts: n/a
 
      7th Nov 2005
Sorry for not mentioning it.

I get an unexpected end of file error.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ODBC SQL Error Bulk Insert - Null values JohnH Microsoft Access External Data 1 21st Jul 2009 12:22 AM
Re-Insert Null value into database field Greyhound Microsoft Access Forms 3 26th Mar 2009 06:29 PM
EXEC sp w/ BULK INSERT - problem w/ security NKTower Microsoft Access ADP SQL Server 2 27th Sep 2008 04:49 PM
insert a null value into a datetime field Esperanza Microsoft ADO .NET 1 7th Dec 2005 03:58 PM
Bulk Insert Problem =?Utf-8?B?U3RldmVI?= Microsoft VB .NET 1 25th Jan 2004 04:24 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:59 AM.