我正在使用 SqlBulkCopy 将 Excel 数据导入 mssql。我在Excel中有很多行数据,有2列,内容有换行和回车(我从下面的在线阅读中了解到是真的)
Chr(10) == vbLF == Line Feed (LF)
Chr(13) == vbCR == Carriage Return (CR)
Chr(13) & Chr(10) == vbCrLf == Carriage Return/Line Feed
SqlBulkCopy 工作正常,但我想保留导入到 mssql 时 Excel 单元格中的回车/换行。目前它作为一个长字符串插入。
示例:其中一个单元格的内容如下 [单个单元格中的多行]。
1st line in cell 1
2nd line in cell 1
当我使用 SqlBulkCopy 将 Excel 数据导入 mssql 时,上面的状态列插入为
"1st line in cell 12nd line in cell 1"
如何保留 Excel 单元格中的“回车/换行”,以便将其插入数据库中,如下所示?
1st line in cell 1
2nd line in cell 1
下面是我的代码。
Dim conectionstring As String = ""
If strExt.ToLower() = ".xls" Then
conectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Excel & ";Extended Properties=Excel 8.0"
ElseIf strExt.ToLower() = ".xlsx" Then
conectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Excel & ";Extended Properties=Excel 12.0"
End If
Dim ExcelConnection As New System.Data.OleDb.OleDbConnection(conectionstring)
ExcelConnection.Open()
Dim expr As String = "SELECT * FROM [Sheet1$] where not U_Id is null"
Dim objCmdSelect As OleDbCommand = New OleDbCommand(expr, ExcelConnection)
Dim objDR As OleDbDataReader
Dim SQLconn As New SqlConnection()
SQLconn.ConnectionString = ConnString
SQLconn.Open()
Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(SQLconn)
bulkCopy.DestinationTableName = "SL_DataInfo_Temp"
bulkCopy.ColumnMappings.Add("U_Id", "di_id")
bulkCopy.ColumnMappings.Add("File_Ref", "di_fileRef")
bulkCopy.ColumnMappings.Add("Date", "di_date")
bulkCopy.ColumnMappings.Add("Status", "di_status")
objDR = objCmdSelect.ExecuteReader
If objDR.HasRows Then ''And objDR.FieldCount >= 13 Then
bulkCopy.WriteToServer(objDR)
ExcelConnection.Close()
SQLconn.Close()
End If
End Using