OdbcDataReader Sqlite DB reading strange results

This method is returning some strange results and was wondering if someone could explain why this is happening and maybe a solution to get my desired results.

Results:

FileName = what i expect

FileSize = what I expect

Buffer = all bytes = 0

BytesRead = 0

BlobString = binary data string

FieldType = BLOB (what I would expect)

ColumnType = System.String

In addition, if the file exceeds several kilobytes, the reader will throw an exception indicating that the StringBuilder's capacity argument must be greater than zero (presumably because the size is larger than Int32.MaxValue).

I guess my question is how to read large BLOB files from OdbcDataReader correctly?

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

      

+1


source to share


2 answers


I created the field as a BLOB. However, after seeing the results of your suggestion of GetFieldType being System.String, I'm not sure. I am using SQLite Manager FireFox addon for manager and it reports content as BLOB.

.NET and SQLite Manager seem to be conflicting. I can save the file properly from the manager, so I know it is stored correctly - it just reads it in my application.

filesize is a textbox, this was what I just added to try and debug the whole thing, and I plan on changing it sooner or later.

The size problem is really surprising to me, but I can't explain it (that's why I'm here :) I can't figure out what the exact size limit is, but I know it will throw an error with a file that's only 34KB. Below is a copy of the exception report I am generating.

An error occured: 1:36 PM 1/4/2009 HelpLink:



InnerException:

Message:
"capacity" must be greater than zero. Parameter name: capacity

Source:
mscorlib

StackTrace:
 at System.Text.StringBuilder..ctor (String value, Int32 startIndex, Int32 length, Int32 capacity) at System.Text.StringBuilder..ctor (String value, Int32 capacity) at System.Data.Odbc.OdbcDataReader.internalGetString (Int32 i) at System.Data.Odbc.OdbcDataReader.GetValue (Int32 i, TypeMap) at System.Data.Odbc.OdbcDataReader.GetValue (Int32 i) at System.Data.Odbc.OdbcDataReader.get_Item (string value) at AppEx.Data.DatabaseHandler.SaveBinaryFile (String Key) at ... \ Data \ DatabaseHandler.cs: line 249

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

+1


source


Is the field type in the database a specific BLOB and not a CLOB? Of course, it looks like it was treated as text data instead of binary data. What returns Reader.GetFieldType(2)

?

As a side issue, is the filesize field really a string and not an integer? Can't you use Reader.GetInt32(1)

?



Finally, with your size issue - when you talk about something "more than a few kilobytes", there is a big difference between "more than a few K" and "big enough to overflow int.MaxValue

" (which would be 2 GB). Do you have something a few megabytes in size?

0


source







All Articles