title: Read a large SQL script file in c#
body: <p>I am trying to read a large script, thus far I have tried two options:</p>
  
  <p><strong>Option 1:</strong></p>
  
  <p>We can't open large script files in SQL management studio because of the issue of out of memory space, so Initially I used <code>sqlcmd</code> to execute 160 mb SQL script file on remote host, after 55 minutes some rows were effected with this error, <code>TCP Provider: An existing connection was forcibly closed by the remote host. , communication link failure.</code></p>
  
  <p><strong>Option 2:</strong></p>
  
  <p>Now I am trying using <a href="https://stackoverflow.com/a/40830/2218697">this</a> example, the file size is 160 MB with lot of insert statements, but Visual Studio crashes</p>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>public ActionResult Index()
  {
     string scriptDirectory = "e:\\";
     string sqlConnectionString = "Integrated Security=SSPI;" +
     "Persist Security Info=True;Initial Catalog=TestDB;Data Source=localhost\\SQLEXPRESS";
     DirectoryInfo di = new DirectoryInfo(scriptDirectory);
     FileInfo[] rgFiles = di.GetFiles("*.sql");
     foreach (FileInfo fi in rgFiles)
     {
          FileInfo fileInfo = new FileInfo(fi.FullName);
          string script = fileInfo.OpenText().ReadToEnd(); // here visual studio crashes
          SqlConnection connection = new SqlConnection(sqlConnectionString);
          Server server = new Server(new ServerConnection(connection));
          server.ConnectionContext.ExecuteNonQuery(script);
     }
  
         return View();
  }
  </code></pre>
  
  <p><strong>Screen Shot:</strong></p>
  
  <p><a href="https://i.stack.imgur.com/Qoe9P.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Qoe9P.png" alt="enter image description here"></a></p>