English 中文(简体)
C# 中的 SQLite 使用. GetBytes 扔出“ 无效Cast- 例外 ” 。 GetBytes
原标题:SQLite in C# throws "InvalidCastException" using .GetBytes
  • 时间:2012-05-24 22:34:29
  •  标签:
  • c#
  • sqlite

我试图在 SQLite DB 中访问一个 Blob s 的柱子, 它最终会指向一个文件或内存的记录。 我试图在 SQLite 中使用. GetBytes 方法来获取一组能代表我数据的字节。 我使用这个方法时不断得到一个无效的例外。 一切似乎都已经就绪,程序编译得很好,但在运行期间,这个例外不断被丢弃。 我四处寻找答案,一切似乎都同意我拥有的代码,所以我损失了这个代码。 不幸的是,我在C# 中找不到任何关于 SQLite 的好文件。 代码如下:

  public byte[] Output()
    {
        byte[] temp = null;
        int col = Columns + 1;
        if(read.Read())
        {
            read.GetBytes(col, 0, temp, 0, 2048); //exception is thrown here
        }
        return temp;
    }

我读过DB里的其他栏目, 它们是插图和文本, 但出于某种原因,

新的信息, 这是来自例外的堆叠追踪 :

     StackTrace:
   at System.Data.SQLite.SQLiteDataReader.VerifyType(Int32 i, DbType typ)
   at System.Data.SQLite.SQLiteDataReader.GetBytes(Int32 i, Int64 fieldOffset, Byte[] buffer, Int32 bufferoffset, Int32 length)
   at SQLiteSort.Sort.Output() in C:UserscjonesDocumentsVisual Studio 2010ProjectsConsoleApplication1ConsoleApplication1Sort.cs:line 192
   at SQLiteSort.Sort.Main(String[] args) in C:UserscjonesDocumentsVisual Studio 2010ProjectsConsoleApplication1ConsoleApplication1Sort.cs:line 72
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

内置例外 :

SQLiteDataReader. VerifyType () 似乎抛出例外, 它正在查看 DbType. Binary 类型所使用的列, 如果该列不是 DbType. Binary、 DbType. String 或 DbType. Guid 。 我反复检查了表格, 并且仍然将列类型显示为 blob 。

最佳回答

我终于通过使用参数s 和将我的布布布设置为 DbType. Binary 来解决这个问题。 不知道为什么这和我之前所做的有什么不同, 但是现在一切都正常了。

问题回答

它抛出一个 InvalidCastExpendion 无疑是错误的,但您的代码 is broken - temp 将是无效的, 这当然不应该是无效的。 您没有在任何地方提供数据读取 。

您重新 不注意 GetBytes 的返回值, 我期待您...

EDIT:为检查... 您 are 试图从合适的栏目中读取, 对不对? InvalidCastexpendion 如果您试图在整数栏上调用 GetBytes , 将会有些适当 。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...