如何在数据库中存储多行文字

本文关键字:文字 存储 数据库 | 更新日期: 2023-09-27 17:59:39

这是我的代码:

public static String ReplaceNewLines(String source, bool allowNewLines)
{
     if (allowNewLines)
          source = source.Replace("'r'n", "<br />");
     else
          source = source.Replace("'r'n", " ");
     return source;
}
protected void btnSubmit_onClick(object sender, EventArgs e)
{
     String txtInput = ReplaceNewLines(txtQuestion.Text, true);
     String connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
     using (SqlConnection conn = new SqlConnection(connectionString))
     {
          // Sql Commands here...
     } 
}

当点击上面的按钮时,数据被插入到数据库中,如下所示:

Test line one. <br /> test line two.

我想要的是它看起来像这样:

Test line one.<br />Test line two.

如何设置代码隐藏,使<br />不会显示在SQL查询中,并在从数据库调用它时在页面中创建另一行?

如何在数据库中存储多行文字

您可以尝试:

source = source.Replace("'r'n", "" + ((char)13).ToString() + (char)10).ToString();

这些是回车符和换行符。

您正在存储正确的信息。如果在浏览器中显示结果(例如,谁可以解释hmtl),则break(<br/>)将为您提供一条新行。

添加"回车"answers"换行"字符,参见示例

选择"这是注释行1"+CHAR(13)+CHAR(10)+"这是行2"

输出

这是评论行1这是第2行