OdbcDataReader读取Sqlite DB的结果很奇怪

时间:2009-01-04 18:53:33

标签: c# sqlite blob datareader

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

结果:

FileName =我期待的是什么

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;
        }
    }

2 个答案:

答案 0 :(得分:1)

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

.NET和SQLite管理器似乎存在冲突。我可以将文件正确地保存在管理器之外,这样我就知道它已正确存储 - 它只是将其读入我的应用程序。

filesize 是一个文本字段,这是我刚刚添加的东西,试图调试整个事情,我计划迟早改变它。

尺寸问题对我来说真的很令人惊讶,但我无法解释它(这就是为什么我在这里:)我无法确定确切的大小限制是什么,但我知道它会在文件中引发错误那只有34KB。下面附有我生成的异常报告的副本。

错误发生:2009年1月4日下午1:36 HelpLink:

InnerException:

消息:
'容量'必须大于零。 参数名称:容量

来源:
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 typemap)    在System.Data.Odbc.OdbcDataReader.GetValue(Int32 i)    at System.Data.Odbc.OdbcDataReader.get_Item(String value)    位于... \ Data \ DatabaseHandler.cs中的AppEx.Data.DatabaseHandler.SaveBinaryFile(String Key):第249行

TargetSite:
Void .ctor(System.String,Int32,Int32,Int32)

答案 1 :(得分:0)

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

正如一个副问题, filesize 字段真的是字符串而不是整数吗?你能不能只使用Reader.GetInt32(1)

最后,随着你的尺寸问题 - 当你谈到“超过几KB”的事情时 - “超过几K”和“足够大以溢出int.MaxValue”(其中将是2GB)。你有几兆的东西吗?

相关问题