KeyNotFoundException:SQLCE 数据库异常

Posted

技术标签:

【中文标题】KeyNotFoundException:SQLCE 数据库异常【英文标题】:KeyNotFoundException: SQLCE Database Exception 【发布时间】:2009-09-07 13:34:17 【问题描述】:

我在 C# 中使用数据集。我已经直接计算了项目的数量 SQL CE 数据库,所以我知道数据库中有多少项。数据库中没有删除。

我收到以下无法解释的异常:

System.Data.SqlServerCe.dll 中发生了“System.Data.SqlServerCe.SqlCeException”类型的第一次机会异常

“System.Collections.Generic.KeyNotFoundException”类型的第一次机会异常发生在 mscorlib.dll KeyNotFoundException 中

这表明了与 SQL CE 数据库的通信锁定的位置。

有谁知道这是什么原因以及如何解决这个问题?

我在网上搜索过,对此没有任何解决方案:-(

        // List to be created
        List<TableDat> result = null;
        lock (_sqlcn)
        
            // Assumes that connection is a valid SqlConnection object.
            SqlCeDataAdapter adapter = new SqlCeDataAdapter(orderSQL, _sqlcn);
            try
            
                // DataSet that will be returned.
                System.Data.DataSet ds = new System.Data.DataSet();
                int count = adapter.Fill(ds, currentIndex, PageSize, "Logs");


                //Iterate through the table and in the List 
                foreach (System.Data.DataRow myDataRow in ds.Tables["Logs"].Rows)
                
                    if (result == null)
                    
                        result = new List<TableDat>();
                    
                    result.Add(
                              new TableDat(
                                  (int)myDataRow[Resources.ID],
                                  (DateTime)myDataRow[Resources.TimeStamp],
                                  (int)myDataRow[Resources.EVENTLOG_SID],
                                  (int)myDataRow[Resources.EVENTLOG_EID])
                              );

                
                // Set the dataSet object to NULL : No longer needed as the list 
                // has  been populated
                ds = null;
            

我看到的最大问题是这会使 SQL CE 数据库停止工作。 Soz,我无法确定我应该在此处的异常处理中做什么,因为我的列表中没有任何内容,这意味着调用者没有列表的详细信息??

这只是表现出来。我正在寻找一种可以处理异常的解决方案,同时也想知道我的数据库为什么会这样:-(

堆栈跟踪看起来像:

A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.IO.IOException' occurred in System.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.IO.IOException' occurred in System.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.IO.IOException' occurred in System.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.IO.IOException' occurred in System.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.IO.IOException' occurred in System.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.IO.IOException' occurred in System.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
A first chance exception of type 'System.Data.SqlServerCe.SqlCeException' occurred in System.Data.SqlServerCe.dll
A first chance exception of type 'System.Data.SqlServerCe.SqlCeException' occurred in System.Data.SqlServerCe.dll
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
A first chance exception of type 'System.Collections.Generic.KeyNotFoundException' occurred in mscorlib.dll
KeyNotFoundException
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll

【问题讨论】:

我还将包括两个异常的完整堆栈跟踪 顺便说一句 - 不需要将 ds 分配给 null (实际上会导致性能下降) DataSet 也实现了 IDisposable 所以你应该把它放到 using 块中 锁定不受您控制的对象也是一种不好的做法。您不知道其他人是否使用相同的对象进行锁定(尽管这也是不好的做法)并且可能导致死锁 System.Data.SqlServerCe.dll 中出现“System.Data.SqlServerCe.SqlCeException”类型的第一次机会异常 System.Data.SqlServerCe.dll 中出现“System.Data.SqlServerCe.SqlCeException”类型的第一次机会异常.Data.SqlServerCe.dll mscorlib.dll 中出现“System.FormatException”类型的第一次机会异常 mscorlib.dll 中出现“System.FormatException”类型的第一次机会异常“System.Collections.Generic”类型的第一次机会异常.KeyNotFoundException'发生在 mscorlib.dll KeyNotFoundException 【参考方案1】:

通常您不必担心第一次机会异常。它们通常由 dll 中的较低级别代码预期和处理。

关于第一次机会异常here 有一篇不错的文章。

您也可以在 Visual Studio 中disable these messages。 (对于 VS 2005...不确定 2008)。

【讨论】:

没有更改,因为我已经取消了将所有输出窗口文本重定向到即时窗口。一旦我得到异常,它就会停止数据库访问。 请您进一步解释一下?这些是什么原因造成的??我怎样才能摆脱它们??

以上是关于KeyNotFoundException:SQLCE 数据库异常的主要内容,如果未能解决你的问题,请参考以下文章

为啥 Dictionary[index] 会抛出 KeyNotFoundException 而 Hashtable[index] 不会?

AspNet Core WebApi 在启动时失败并出现错误 System.Collections.Generic.KeyNotFoundException

c#多键字典,用自己的可变类替换元组时出现“KeyNotFoundException”[重复]

System.Collections.Generic.KeyNotFoundException:从 PUSHSHARP 发送推送通知时,字典中不存在给定的键

System.Collections.Generic.KeyNotFoundException: 给定关键字不在字典中。

workitem.open() KeyNotFoundException TFS 2013