【问题标题】:Strange results from OdbcDataReader reading Sqlite DBOdbcDataReader 读取 Sqlite DB 的奇怪结果
【发布时间】:2010-09-29 12:22:26
【问题描述】:

此方法返回一些奇怪的结果,想知道是否有人可以解释为什么会发生这种情况,并且可能是获得我想要的结果的解决方案。

结果:

文件名 = 我的期望

FileSize = 我的期望

缓冲区 = 所有字节 = 0

BytesRead = 0

BlobString = 二进制数据字符串

FieldType = BLOB(我所期望的)

ColumnType = System.String

此外,如果文件大于几 KB,阅读器会抛出异常,指出 StringBuilder 容量参数必须大于零(可能是因为大小大于 Int32.MaxValue)。

我想我的问题是如何正确地从 OdbcDataReader 读取大型 BLOB?

    public static String SaveBinaryFile(String Key)
    {
        try
        {
            Connect();

            OdbcCommand Command = new OdbcCommand("SELECT [_filename_],[_filesize_],[_content_] FROM [_sys_content] WHERE [_key_] = '" + Key + "';", Connection);
            OdbcDataReader Reader = Command.ExecuteReader(CommandBehavior.SequentialAccess);

            if (Reader.HasRows == false)
                return null;

            String FileName = Reader.GetString(0);
            int FileSize = int.Parse(Reader.GetString(1));
            byte[] Buffer = new byte[FileSize];
            long BytesRead = Reader.GetBytes(2, 0, Buffer, 0, FileSize);

            String BlobString = (String)Reader["_content_"];
            String FieldType = Reader.GetDataTypeName(2);
            Type ColumnType = Reader.GetFieldType(2);

            return null;
        }
        catch (Exception ex)
        {
            Tools.ErrorHandler.Catch(ex);
            return null;
        }
    }

【问题讨论】:

    标签: c# sqlite blob datareader


    【解决方案1】:

    数据库中的字段类型肯定是BLOB而不是CLOB吗?它确实看起来好像将其视为文本数据而不是二进制数据。 Reader.GetFieldType(2) 返回什么?

    作为一个附带问题,filesize 字段真的是字符串而不是整数吗?不能只用Reader.GetInt32(1)吗?

    最后,关于您的大小问题 - 当您谈论“超过几 KB”时 - “超过几 K”和“大到足以溢出 int.MaxValue”之间存在很大差异(这将是2GB)。你有几兆的东西吗?

    【讨论】:

      【解决方案2】:

      我将该字段创建为 BLOB。但是,看到您建议 GetFieldType 为 System.String 的结果,我不确定。我正在将 SQLite Manager FireFox 插件用于管理器,并将 content 报告为 BLOB。

      .NET 和 SQLite 管理器似乎有冲突。我可以将文件从管理器中正确保存,因此我知道它已正确存储——它只是将它读入我的应用程序中。

      filesize 是一个文本字段,我只是快速添加它以尝试调试整个事情,我计划迟早更改它。

      大小问题让我很惊讶,但我无法解释(这就是我在这里的原因:)我无法确定确切的大小限制是多少,但我知道它会引发文件错误那只有34KB。下面附上我生成的异常报告的副本。

      发生错误:2009 年 1 月 4 日下午 1:36 帮助链接:

      内部异常:

      留言:
      “容量”必须大于零。 参数名称:容量

      来源:
      mscorlib

      堆栈跟踪:
      在 System.Text.StringBuilder..ctor(字符串值,Int32 startIndex,Int32 长度,Int32 容量) 在 System.Text.StringBuilder..ctor(字符串值,Int32 容量) 在 System.Data.Odbc.OdbcDataReader.internalGetString(Int32 i) 在 System.Data.Odbc.OdbcDataReader.GetValue(Int32 i,TypeMap 类型映射) 在 System.Data.Odbc.OdbcDataReader.GetValue(Int32 i) 在 System.Data.Odbc.OdbcDataReader.get_Item(字符串值) 在 AppEx.Data.DatabaseHandler.SaveBinaryFile(String Key) in ...\Data\DatabaseHandler.cs:line 249

      目标站点:
      void .ctor(System.String, Int32, Int32, Int32)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-28
        相关资源
        最近更新 更多