c# - 试图读取或写入 protected 内存 - Sql Compact 和 .NEt

标签 c# .net winforms sql-server-ce

我曾尝试在两种情况下运行该应用程序:安装了 SQL Server 精简版 (3.5) 的情况和一种未安装的情况。未安装 SQL Server Compact 版本时,出现以下错误:

Exception :Attempted to read or write protected memory. This is often an indication
that other memory is corrupt.

Inner Exception : 
========================

Stack Trace :
========================
   at System.Data.SqlServerCe.NativeMethods.CloseStore(IntPtr pSeStore)
   at System.Data.SqlServerCe.SqlCeConnection.ReleaseNativeInterfaces()
   at System.Data.SqlServerCe.SqlCeConnection.Dispose(Boolean disposing)
   at System.Data.SqlServerCe.SqlCeConnection.Finalize()
Source : 
========================
System.Data.SqlServerCe
----------------------------------------------------------

我正在使用 this method 处理未处理的异常.

我在从 Windows 窗体启动的控制台应用程序中收到此错误。在这两个应用程序中,我都插入了未处理的异常编码,它正在执行并返回到文本文件,并且正在生成 Microsoft 的“报告错误”对话框;我如何防止这种情况发生?如何排查导致此问题的原因?

最佳答案

当我遇到这个问题时,问题出在数据库连接上:

如果您在重新打开连接之前不关闭连接,您会在“System.Data.SqlServerCe”附近收到此错误。

只需添加这一行就解决了我的问题:

SqlCeConnection conn = new SqlCeConnection(_connectionString);
conn.Close(); //adding this.


if(conn == ConnectionState.Open) {
//-- Your code here.
}

关于c# - 试图读取或写入 protected 内存 - Sql Compact 和 .NEt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2423458/

相关文章:

c# - 使用 sender 参数而不是直接访问所需的控制有什么好处

c# - 如何在 ListView 中创建可点击的链接?

c# - 如何将任意方法(或委托(delegate))作为参数传递给函数?

c# - Outlook.MailItem.UserProperty 发送邮件后消失

c# - 需要在 Unity 4.3.4 中使用 Tiled Map editor

.net - 如何将 System.Color 转换为 Microsoft Word WdColor?

c# - ISponsor 和 ILease 接口(interface)如何工作?

.net - CNTK.CPUOnly在Docker中[Windows]

C# 文件路径帮助

c# - 托管代码能否像非托管代码一样快速地执行计算?