473,395 Members | 2,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Using SqlBulkCopy with Line Feed and Carriage Return

4
I am using SqlBulkCopy to import excel data to mssql.
I have many rows of data in excel with 2 columns which the content has Line Feed and Carriage Return (I understand from reading online below is true)

Chr(10) == vbLF == Line Feed (LF)
Chr(13) == vbCR == Carriage Return (CR)
Chr(13) & Chr(10) == vbCrLf == Carriage Return/Line Feed

The SqlBulkCopy is working perfectly but i want to retain the Carriage Return/Line Feed which is in the excel when its imported to mssql.Curently it is inserted as a long string .

Example below .

One of the cell has this content

this is 1st line
this is 2nd line

When i use SqlBulkCopy to import excel data to mssql.The above is inserted as "this is 1st linethis is 2nd line "

How can i retain the "Carriage Return/Line Feed" from the excel cell ? Please help.Thank you.
Aug 20 '13 #1
5 3070
anusia
4
to add: Iam using vb.net for the above .
Aug 20 '13 #2
anusia
4
Below is the codes for what i have now

Expand|Select|Wrap|Line Numbers
  1.    Dim conectionstring As String = ""
  2.             If strExt.ToLower() = ".xls" Then
  3.                 conectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Excel & ";Extended Properties=Excel 8.0"
  4.             ElseIf strExt.ToLower() = ".xlsx" Then
  5.                 conectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Excel & ";Extended Properties=Excel 12.0"
  6.             End If
  7.     Dim ExcelConnection As New System.Data.OleDb.OleDbConnection(conectionstring)
  8.             ExcelConnection.Open()
  9.     Dim expr As String = "SELECT * FROM [Sheet1$] where not U_Id is null"
  10.     Dim objCmdSelect As OleDbCommand = New OleDbCommand(expr, ExcelConnection)
  11.     Dim objDR As OleDbDataReader
  12.     Dim SQLconn As New SqlConnection()
  13.  
  14.             SQLconn.ConnectionString = ConnString
  15.             SQLconn.Open()
  16.  
  17.                Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(SQLconn)
  18.  
  19.                 bulkCopy.DestinationTableName = "SL_DataInfo_Temp"
  20.                 bulkCopy.ColumnMappings.Add("U_Id", "di_id")
  21.                 bulkCopy.ColumnMappings.Add("File_Ref", "di_fileRef")
  22.                 bulkCopy.ColumnMappings.Add("Date", "di_date")
  23.                 bulkCopy.ColumnMappings.Add("Status", "di_status")
  24.  
  25.                 objDR = objCmdSelect.ExecuteReader
  26.  
  27.                 If objDR.HasRows Then ''And objDR.FieldCount >= 13 Then
  28.                     bulkCopy.WriteToServer(objDR)
  29.                     ExcelConnection.Close()
  30.                     SQLconn.Close()
  31.                 End If
  32.              End Using
Aug 20 '13 #3
Rabbit
12,516 Expert Mod 8TB
It probably transferred just fine, it's simply that the SSMS result set doesn't print multi-line characters to the screen. If you want to make sure it's there, just substring the character that the CR or LF is supposed to be at and return the ASCII value.
Aug 20 '13 #4
anusia
4
Thanx Rabbit.your right.. the SSMS result set doesn't print multi-line characters to the screen.I used the below query to to check if it was actually there in DB .


SELECT di_borrower
FROM SL_DataInfo
WHERE (di_borrower LIKE '%' + CHAR(10)+ '%')

AND HAPPY ME !! IT WAS THERE IN DB ! So the sqlbulkinsert did insert correctly after all.

Then just to share my solution, what i did to fix it was.

#1 - replace the data with </br>

update dbo.SL_DataInfo_Temp set
di_borrower = replace(cast(di_borrower AS nvarchar(250)), char(10), '<br />')

#2 - Change the gridview column HTML Encode = False.

This made the gridview display the Carriage Return correctly .

Thanx for the input Rabbit ! :)
Aug 20 '13 #5
Rabbit
12,516 Expert Mod 8TB
Glad you got it working, good luck with the rest of your project.
Aug 20 '13 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Canes_Rock | last post by:
The information posted at: ...
5
by: stephane | last post by:
Hi, I don't know how to test something. I read at file.txt caracter by caracter And I would like to know when it is the end of my line in the text file. exemple of a line: ...
10
by: Geoff Cox | last post by:
Hello, How do I get a carriage return after each of the names?! Cheers Geoff <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
1
by: wschaub | last post by:
Is there any way of forcing an ASMX web service not to translate a carriage return – line feed (\r\n) with a (\n\n), in other words the carriage return \r is replaced with a \n if contained as...
3
by: David N. | last post by:
Hi All, I spent too much time on trying to get the CrLf into a string, which contains embedded SQL statements that can be executed by the SQLClient.SqlCommand. Note that these SQL statements...
1
by: Jim Heavey | last post by:
In the Regular Expression, I can use \n for Line Feed, \t for a tab and \r for carriage return. Am I able to use thos same expressions in a "Replace method" of the string object? such as ...
2
by: sys | last post by:
I have a web method that returns a string. When I return a string that has a carriage return and line feed in it ("\r\n") on the client side I only receive a string including the line feed ("\n")....
11
by: TheRain | last post by:
Hi, I am trying to append a carriage return to my string using the string builder class, but when I do this the string ends up containing "13". I tried this multiple ways like so ...
4
by: whitej77777 | last post by:
I am trying to write a user defined function that will allow me to strip off the last carriage return and line feed from a text field. We have address fields stored in a text field for our ERP...
4
by: coolguyraj | last post by:
Hi. I have form that has a text box. I want insert unformatted text into the database. Even if the user gives an carriage return or New line feed in the database it should be stored as on single...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.