【问题标题】:Newlines get stripped in SQL after datatable update数据表更新后,换行符在 SQL 中被剥离
【发布时间】:2012-05-24 15:48:44
【问题描述】:

我有一个通过删除 html 来修改的数据表。在 html 条带期间,如果遇到 <br><p><li>,它们将替换为 System.Environment.NewLine。我正在将 html strip 进程的实例记录到文本文件中,并且格式在日志中看起来很好(所有 CRLF 都被保留)。但是,当在数据表上调用更新方法并将数据发送到数据库时,CRLF 字符都消失了。

代码sn-p:

public static class HtmlStripper
{
    static Regex _htmlRegex = new Regex("<.*?>", RegexOptions.Compiled);
    static Regex _liRegex = new Regex("<li>", RegexOptions.Compiled);
    static Regex _brRegex = new Regex("<(br)?(BR)?\\s?/?>\\s*", RegexOptions.Compiled);
    static Regex _pRegex = new Regex("</?[phPH].*?>\\s*", RegexOptions.Compiled);

    public static string StripTagsRegexCompiled(string source)
    {
        string noPorH = _pRegex.Replace(source, System.Environment.NewLine);
        string noBr = _brRegex.Replace(noPorH, System.Environment.NewLine);
        string noLi = _liRegex.Replace(noBr, System.Environment.NewLine + "t- ");
        return _htmlRegex.Replace(noLi, string.Empty);
    }

}

【问题讨论】:

  • 一些代码片段将有助于回答您的问题。
  • 您是否正在使用 Sql Server Management Studio 检查数据库中的数据?如果是这样,它会在显示时去掉换行符(但数据仍然正确)
  • 我将数据从SSMS复制到Notepad++,CRLF字符都不见了。
  • @DanAndrews 那么你是说如果我在应用程序中使用这些数据会保留换行符吗?
  • 所以是的,毕竟它是 SSMS。我从应用程序中查询了数据库并显示了数据,并且 CRLF 字符在那里。你每天都会学到一些东西。

标签: c# sql-server newline


【解决方案1】:

SSMS 删除了数据网格中的 CRLF - 但是它们仍然存在于数据库中。您可以通过按CTRL-T 来证明这一点,这会将您的结果转换为 TEXT 而不是在 GRID 中。然后,您可以按 CTRL-D 以返回将结果保存在 GRID 中。

您可以使用以下方法进行测试:

SELECT  'Not' + CHAR(10) + CHAR(13) + 'Together'

首先在 GRID 中显示结果:

(No Column name)
Not  Together

然后在TEXT结果中:

SQL Server parse and compile time: 
   CPU time = 0 ms, elapsed time = 0 ms.

-------------
Not

Together

(1 row(s) affected)


SQL Server Execution Times:
   CPU time = 0 ms,  elapsed time = 0 ms.
SQL Server parse and compile time: 
   CPU time = 0 ms, elapsed time = 0 ms.

SQL Server Execution Times:
   CPU time = 0 ms,  elapsed time = 0 ms.

【讨论】:

    猜你喜欢
    • 2012-11-22
    • 2011-03-23
    • 1970-01-01
    • 2011-07-16
    • 2013-12-06
    • 2016-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多