SQL 带回车的表中的数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/139670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Data in a table with carriage return?
提问by user21968
In SQL SERVER Is it possible to store data with carriage return in a table and then retrieve it back again with carriage return.
在SQL SERVER中是否可以将带有回车的数据存储在表中,然后用回车再次检索它。
Eg:
例如:
insert into table values ('test1
test2
test3
test4');
When I retrieve it, I get the message in a line
当我检索它时,我将消息排成一行
test1 test2 test3 test4
测试1 测试2 测试3 测试4
The carriage return is treated as a single character.
回车被视为单个字符。
Is there way to get the carriage returns or its just the way its going to be stored?
有没有办法让回车或它的存储方式?
Thanks for the help guys!!!
感谢大佬们的帮助!!!
Edit: I should have explained this before. I get the data from the web development (asp .net) and I just insert it into the table. I might not be doing any data manipulation.. just insert.
编辑:我应该之前解释过这一点。我从 Web 开发 (asp .net) 获取数据,然后将其插入表中。我可能不会做任何数据操作.. 只是插入。
I return the data to the app development (C++) and may be some data or report viewer.
我将数据返回给应用程序开发(C++),可能是一些数据或报告查看器。
I don't want to manipulate on the data.
我不想操纵数据。
回答by George Mastros
You can store Carriage return in the database. The problem here is that you are using SQL Server Management Studio to display the results of your query. You probably have it configured to show the results in a grid. Change the configuration of SSMS to show results to text and you will see the carriage returns.
您可以将回车存储在数据库中。这里的问题是您使用 SQL Server Management Studio 来显示查询结果。您可能已将其配置为在网格中显示结果。更改 SSMS 的配置以将结果显示为文本,您将看到回车符。
Right click in the query window -> Results To -> Results To Text
在查询窗口右击 -> Results To -> Results To Text
Run your query again.
再次运行您的查询。
回答by huo73
INSERT INTO table values('test1' + CHAR(10) + 'test2' + CHAR(10) + 'test3' + CHAR(10) + 'test4')
This should do it. To see the effect, switch the query result window to plain text output.
这应该这样做。要查看效果,请将查询结果窗口切换为纯文本输出。
Regards
问候
回答by Axeman
IIRC, using chr(13) + chr(10) should works.
IIRC,使用 chr(13) + chr(10) 应该可以工作。
insert into table values ('test1' + chr(13) + chr(10) + 'test2' );
回答by Dejan
Can you please clarify how you retrieve the data back from the database? What tool do you use? The data probably contains the carriage returns but it's not displayed if you get the results in grid (try the results in text option)
您能否澄清一下如何从数据库中检索数据?你用什么工具?数据可能包含回车,但如果您在网格中获得结果,则不会显示它(尝试文本选项中的结果)
回答by Neall
You might need to put in a "\n" instead of a literal carriage return.
您可能需要输入“\n”而不是文字回车。
回答by Learning
The carriage return is stored as is. The problem here is that your sql client is not understanding it. If you did a raw dump of this data you'll see that the carriage returns are there in the data.
回车按原样存储。这里的问题是您的 sql 客户端不理解它。如果您对该数据进行了原始转储,您将看到数据中存在回车符。
I use DBArtisan at work and it seems to work fine. However isql seems to have the same problem that you reported.
我在工作中使用 DBArtisan,它似乎工作正常。但是 isql 似乎有您报告的相同问题。
回答by Cirieno
Is this result in your HTML or in Query analyser? If it's in HTML, have a look at the source code and it might appear correct there, in which case you'd have to replace the crlf characters with <br />
tags.
这是在您的 HTML 中还是在查询分析器中?如果它在 HTML 中,请查看源代码,它可能在那里看起来是正确的,在这种情况下,您必须用<br />
标签替换 crlf 字符。
I'm also thinking that there used to be attributes you could add to an HTML textarea to force it to send carriage returns in certain ways -- soft or hard? I haven't looked that up, perhaps someone could do that.
我还认为曾经有一些属性可以添加到 HTML 文本区域以强制它以某种方式发送回车——软的还是硬的?我没查过,也许有人可以做到这一点。
But SQL Server does save the two characters in my experience. In fact I did exactly as you described here a few days ago using SQL 2005 and each line break has two unprintable characters.
但根据我的经验,SQL Server 确实保存了这两个字符。事实上,我几天前使用 SQL 2005 完全按照您在此处描述的那样进行操作,并且每个换行符都有两个不可打印的字符。
回答by Mario
I am using SQLite to store multiline texbox and got something like that when retrieving data stored and showing it on any object (txtboxes, labels, etc.). Anytime I copied data into NotePad/WordPad or similar, I could see that carriage returns were stored, they simply weren't show in the ASP page.
我正在使用 SQLite 来存储多行 texbox,并在检索存储的数据并将其显示在任何对象(txtboxes、标签等)上时得到了类似的东西。每当我将数据复制到 NotePad/WordPad 或类似工具时,我都可以看到存储了回车,但它们根本没有显示在 ASP 页面中。
Found the answer here:
在这里找到答案:
hope that helps.
希望有帮助。
My code example:
我的代码示例:
C#:
C#:
protected void Page_Load(object sender, EventArgs e)
{
String str = Request.QueryString["idNoticia"];
this.dsNewsDetails.FilterExpression = "idNoticia=" + str;
}
ASPX:
ASPX:
<asp:Label ID="BodyLabel"
runat="server" style="font-size: medium"
Text='<%# Eval("body").ToString().Replace(Environment.NewLine,"<br/>") %>' Width="100%" />
Note: as mentioned in the link provided, Environment.NewLine works both for C# and VB
注意:如提供的链接中所述,Environment.NewLine 适用于 C# 和 VB
回答by Ankit Mahajan
If you switch the output to plain text you can see the data in different lines.
To switch output go to Tools>Options>Query Results
and set default destination to: text.
You can also try hitting Ctrl+p
before executing query. Hope it helps.
如果将输出切换为纯文本,则可以在不同的行中看到数据。要切换输出转到Tools>Options>Query Results
并将默认目标设置为:文本。您也可以Ctrl+p
在执行查询之前尝试点击。希望能帮助到你。