title: Attempted to read or write protected memory connecting to SQL Server
body: <p>This is the error I'm getting:</p>
  
  <blockquote>
    <p>Failed to connect to server MYSERVER. (Microsoft.SqlServer.ConnectionInfo)</p>
    
    <p>ADDITIONAL INFORMATION:</p>
    
    <p>Attempted to read or write protected memory. This is often an indication that other memory is corrupt. (System.Data)</p>
  </blockquote>
  
  <p>I was getting this error while using entity framework to run any query, e.g. any line like this:</p>
  
  <pre><code>var AllRows = context.MyTableRows.ToList();
  </code></pre>
  
  <p>I thought it was an EF problem, but then I tried to use the Transact Sql Editor in VS2012 to connect to the database so I could query it, and I get the same error when trying to connect.</p>
  
  <p>But I've got other, non-Microsoft querying tools that can connect to the database and query it, without causing the error. So I think the problem is somewhere in the Microsoft drivers, or System.Data.SqlClient, but I can't figure out where the problem is.</p>
  
  <p><strong>Update:</strong> I tried connecting in LinqPad to the same server, and when I try to connect it just hangs.</p>
  
  <p><strong>Update:</strong> The stack trace is quite long, and looks like this:</p>
  
  <pre><code>at SNIAddProvider(SNI_Conn* , ProviderNum , Void* )
  at SNINativeMethodWrapper.SNIAddProvider(SafeHandle pConn, ProviderEnum providerEnum, UInt32&amp; info)
  at System.Data.SqlClient.TdsParser.ConsumePreLoginHandshake(Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean&amp; marsCapable)
  at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover)
  at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover)
  at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout)
  at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance)
  at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions)
  at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
  at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
  at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnectionOptions userOptions)
  at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnectionOptions userOptions)
  at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal&amp; connection)
  at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal&amp; connection)
  at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal&amp; connection)
  at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
  at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
  at System.Data.SqlClient.SqlConnection.Open()
  at System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection, String exceptionCode, String attemptedOperation, Boolean&amp; closeStoreConnectionOnFailure)
  at System.Data.EntityClient.EntityConnection.Open()
  at System.Data.Objects.ObjectContext.EnsureConnection()
  at System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
  at System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable&lt;T&gt;.GetEnumerator()
  at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
  at System.Data.Objects.ELinq.ObjectQueryProvider.&lt;GetElementFunction&gt;b__2[TResult](IEnumerable`1 sequence)
  at System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable`1 query, Expression queryRoot)
  at System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[S](Expression expression)
  at System.Data.Entity.Internal.Linq.DbQueryProvider.Execute[TResult](Expression expression)
  </code></pre>
  
  <p>...lines in my app that try to execute an EF query...</p>