问题
I have to use a column with a datatype of TEXT in SQL Server 2005 to store a string that I need formatted with newlines and tab character for example.
prior to calling the stored proc, I added a breakpoint to view the text I'm sending in the text visualizer in VS2010, and all formatting is exactly how I want it to be, but when I go the the cell in the database, and copy out the data, the formatting is lost. From what I can see, every time it a newline character or tab character is present in the string, the text datatype just replaces it with a space. For newlines I'm using vbNewline and for tabs I'm using vbTab or Xml.Formatting.Indented
It's worth note that I can NOT change the data type of the column, because it's simply a table i have to use for a specific purpose.
Any suggestions on getting this to format correctly in the database with at datatype of text?
EXAMPLE:
SQL Table Script Representing the table:
CREATE TABLE [dbo].[FooLog](
[FooID] [int] IDENTITY(1,1) NOT NULL,
[FooInfo] [text] NULL )
Stringbuilder code:
Dim sb As New StringBuilder()
sb.AppendFormat("{0}{1}", "Some text", vbNewLine)
sb.AppendLine("Some foo bar data")
sb.AppendLine(String.Format("{0}data{1}{0}some more data", vbTab, vbNewLine))
Dim stringToStoreInFooInfo As String = sb.ToString()
Store in db:
db.AddInParameter(cmd, "FooInfo", SqlDbType.Text, stringToStoreInFooInfo)
I'm not reading it back with code at the moment... I'm copying it out of the cell with Ctrl-C or right-click "copy"
回答1:
It does preserve the formatting fine, you're losing the format by copying out of the SSMS cell.
Once you've got code pulling the text back out you should still have the formatting in place.
For example if you run the following in SSMS, you'll lose the newlines. It switches in spaces for other whitespace chars I think.
Select
'
This
is
some fomatted
text
'
来源:https://stackoverflow.com/questions/13650672/sql-server-datatype-of-text-does-not-maintain-formatting