BitConverter.ToString(byte [])可以从SQL SMO查询处理的最大长度是多少?

时间:2017-02-01 19:41:52

标签: sql-server-2012 smo

我将数据从SQL Server 2012表中提取到SQL SMO表中,然后循环遍历结果集。执行以下操作时,我在运行时收到内存不足错误:

<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<!-- This is the scrollable area -->
<body data-spy="scroll" data-target=".navbar">
  <!-- The navbar where the anchors <a> are used ot jump to a section in the scrollable container -->
  <nav class="navbar navbar-inverse navbar-fixed-top">
    <div class="container-fluid">
      <div class="navbar-header">
        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span> 
        </button>
       <!-- These are the links placed in the nav across the top -->
      <a class="navbar-brand" href="#">Site</a>
      </div>
      <div class="collapse navbar-collapse" id="myNavbar">
        <ul class="nav navbar-nav">
          <li><a href="#section1">Section1</a></li>
          <li><a href="#section2">Section2</a></li>
          <li><a href="#section3">Section3</a></li>
        </ul>
      </div>
    </div>
  </nav>
  <div>
  <div id="section1" class="container-fluid">
    <h1>Section 1</h1>
    <p>This is some text....try out the nav bar scroll, hopefully it works</p>
    <p>This is some text....try out the nav bar scroll, hopefully it works</p>
  </div>

  <div id="section2" class="container-fluid">
    <h1>Section 2</h1>
    <p>This is some text....try out the nav bar scroll, hopefully it works</p>
    <p>This is some text....try out the nav bar scroll, hopefully it works</p>
  </div>

  <div id="section3" class="container-fluid">
    <h1>Section 3</h1>
    <p>This is some text....try out the nav bar scroll, hopefully it works</p>
    <p>This is some text....try out the nav bar scroll, hopefully it works</p>
  </div>
  </div>
</body>

它不会抛出错误,直到它遇到具有〜15MB(15127052字节)的DATALENGTH([Data])的行。

BitConverter.ToString(byte [])可以处理二进制数据的最大大小吗?

1 个答案:

答案 0 :(得分:0)

它实际上与SMO和数据库完全无关,而且与string.Replace()函数的明显限制有关。我打破了LINQ代码,以便逐步操作数据,并发现当我尝试执行s.Replace("-", "")时,这就是错误的地方。在Stack Exchange上挖掘了一下之后我发现:Does string.Replace(string, string) create additional strings?应用我在那里找到的东西,我将代码从使用字符串更改为使用StringBuilder,然后使用StringBuilder.Replace(string, string)函数并且它可以工作。显然因为string.Replace(string, string)创建源字符串的副本,必须存在某种内存限制,但由于StringBuilder对原始字符串而不是副本进行操作,因此该限制不适用。

最后代码最终看起来像:

StringBuilder sb = new StringBuilder(dr["Data"] == DBNull.Value ? "NULL" : BitConverter.ToString((byte[])dr["Data"]));

sb.Replace("-", "");

感谢您的帮助。