首页 新闻 会员 周边

SqlConnection 连接字符串--求解 System.AccessViolationException: 尝试读取或写入受保护的内存。这通常

0
悬赏园豆:160 [已解决问题] 解决于 2014-03-03 12:33

系统:windows 7 64位,vs2010.

windows 程序,在创建项目的时候,项目默认编译目标平台为X86。

功能:只连接数据库,本机。

第一个字符串为127.0.0.1;

第二个字符串为:.;

 string connectionstring1 = "Data Source=127.0.0.1;Initial Catalog=YRB;User ID=sa;Password=sa;Persist Security Info=True";

            string connectionstring2 = "Data Source=.;Initial Catalog=YRB;User ID=sa;Password=sa;Persist Security Info=True";
  
            using (SqlConnection conn = new SqlConnection(connectionstring1))
            {
                conn.Open();
                conn.Close();
            }

            using (SqlConnection conn = new SqlConnection(connectionstring2))
            {
                conn.Open();
                conn.Close();
            }

问题出来了:

第一个连接字符串在打开连接时报异常,如下:

未处理 System.AccessViolationException
  HResult=-2147467261
  Message=尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
  Source=System.Data
  StackTrace:
       在 SNINativeMethodWrapper.SNIClose(IntPtr pConn)
       在 System.Data.SqlClient.SNIHandle.ReleaseHandle()
       在 System.Runtime.InteropServices.SafeHandle.InternalDispose()
       在 System.Runtime.InteropServices.SafeHandle.Dispose(Boolean disposing)
       在 System.Runtime.InteropServices.SafeHandle.Dispose()
       在 System.Data.SqlClient.TdsParserStateObject.Dispose()
       在 System.Data.SqlClient.TdsParser.Disconnect()
       在 System.Data.SqlClient.SqlInternalConnectionTds.LoginFailure()
       在 System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance)
       在 System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData)
       在 System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
       在 System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
       在 System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
       在 System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
       在 System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
       在 System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
       在 System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
       在 System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
       在 System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
       在 System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
       在 System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
       在 System.Data.SqlClient.SqlConnection.Open()
       在 ConsoleApplication1.Program.Main(String[] args) 位置 C:\Users\WANGJD\Desktop\ConsoleApplication1\ConsoleApplication1\Program.cs:行号 25
       在 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       在 System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

 

第二个正常。

经测试,在连接字符串中无论使用IP地址或域名做为数据库服务器目标时,都会出现以上异常。

----------------------------

但当将项目的目标平台设置为AnyCPU时,所有情况都正常,求解!!!!

补充:上周2月26号,电脑自动更新了好多关于.Net 的部分(包括Vs2010),从此电脑中所有运行正常的项目(连接数据库部分)都出现以上异常,无法运行!!!

深挖三尺的主页 深挖三尺 | 初学一级 | 园豆:130
提问于:2014-03-03 12:03
< >
分享
最佳答案
1

netsh winsock reset   --运行此命令,解决。 ----坑死人啦~~~

百度:

netsh winsock reset命令,作用是重置 Winsock 目录。如果一台机器上的Winsock协议配置有问题的话将会导致网络连接等问题,就需要用netsh winsock reset命令来重置Winsock目录借以恢复网络。这个命令可以重新初始化网络环境,以解决由于软件冲突、病毒原因造成的参数错误问题。 netsh是一个能够通过命令行操作几乎所有网络相关设置的接口,比如设置IP,DNS,网卡,无线网络等,Winsock是系统内部目录,Winsock是Windows网络编程接口,winsock工作在应用层,它提供与底层传输协议无关的高层数据传输编程接口,reset是对Winsock的重置操作。当执行完winsock的命令重启计算机后,需要重新配置IP。

深挖三尺 | 初学一级 |园豆:130 | 2014-03-03 12:32

 在有vs2010后 再安装vs2013 重启主机  访问部署iis上的vs2010项目 就出现以上错误 

  系统日志中也出现

错误应用程序名称: w3wp.exe,版本: 7.5.7601.17514,时间戳: 0x4ce7afa2
错误模块名称: ntdll.dll,版本: 6.1.7601.18229,时间戳: 0x51fb164a
异常代码: 0xc0000005
错误偏移量: 0x000000000004e4e4
错误进程 ID: 0xbe0
错误应用程序启动时间: 0x01d0baaf598cfaf3
错误应用程序路径: c:\windows\system32\inetsrv\w3wp.exe
错误模块路径: C:\Windows\SYSTEM32\ntdll.dll
报告 ID: 97a8ac9c-26a2-11e5-a5a4-f46d043db863

 

就是用以上方式解决 

 

谢谢

this time | 园豆:200 (初学一级) | 2015-07-10 10:08

@this time: 点赞

大锤锤 | 园豆:200 (初学一级) | 2015-10-21 10:04
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册