简体   繁体   English

将换行符从MS SQL Server中存储的多行文本框值转换为HTML

[英]Convert linebreaks from multiline textbox value stored in MS SQL Server to HTML

Ok, I'm storing the value of a multiline textbox in SQL Server in column [txt] of type nvarchar(1000). 好的,我将多行文本框的值存储在SQL Server中nvarchar(1000)类型的[txt]列中。

The multiline text in this case is: 在这种情况下,多行文字为:

Dear sir, 亲爱的先生,

Welcome to our platform. 欢迎来到我们的平台。

Best, Us 最好,我们

After saving the results to MSSQL I run a query to select the values. 将结果保存到MSSQL后,我运行查询以选择值。 When I copy the value of column [txt] directly from the query results in SQL Server Management Studio and paste this into Notepad++, I see the original text again. 当我直接从SQL Server Management Studio的查询结果中复制[txt]列的值并将其粘贴到Notepad ++中时,我会再次看到原始文本。 Also the loading the text again from the database and assigning that to the multiline textbox is working fine. 同样,再次从数据库中加载文本并将其分配给多行文本框也可以正常工作。

However, when in my code behind I assign the value of database column [txt] to a string variable (so not from Textbox to the variable): 但是,在后面的代码中,我将数据库列[txt]的值分配给字符串变量(而不是从文本框分配给该变量):

Dim mailContent as String = row.txt

and then send this value in an email, the email recipient sees the text without the line breaks, so: 然后在电子邮件中发送此值,则电子邮件收件人会看到没有换行符的文本,因此:

Dear sir, Welcome to our platform. 尊敬的先生,欢迎来到我们的平台。 Best, Us 最好,我们

I tried all different scenerios below: 我在下面尝试了所有不同的场景:

Dim registryTA As New registryTableAdapters.registryTableAdapter
Dim registryDT As registry.registryDataTable = registryTA.GetDataByUserid(_userid)
Dim mailcontent As String

mailcontent = registryDT(0).invitetext.Replace(vbLf, "<br/>").Replace(vbCrLf, "<br/>")

mailContent = mailContent.Replace(Environment.NewLine, "<br/>")
mailContent = mailContent.Replace("\r\n", "<br/>")
mailContent = Server.HTMLEncode(mailContent)
mailContent = Server.HTMLDecode(mailContent)
mailContent = mailContent.Replace(vbCrlf, "<br/>")

Preferably I don't want to alter the way I store or retrieve the data in/from MSSQL (like here ), so how can I convert the current value of [txt] to HTML format and get the linebreaks? 最好是,我不想更改在MSSQL中/从MSSQL中存储或检索数据的方式(如此 ),那么如何将[txt]的当前值转换为HTML格式并获取换行符?

Seems like the text might only have \\n in your content. 似乎文本内容中可能只有\\n Try replacing \\n with </br> as well. 尝试将\\n替换为</br> Try this, 尝试这个,

mailContent = mailContent.Replace(vbLF, "<br/>").Replace(vbCrlf, "<br/>")

替换SQL select查询中的换行符

 REPLACE(Account.AcAddress,CHAR(10),'<br/>')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM