in

System.Data.SQLite

An open source ADO.NET provider for the SQLite database engine

Readin Blob data in C#

Last post 06-19-2009 10:21 AM by cheetah. 4 replies.
Page 1 of 1 (5 items)
Sort Posts: Previous Next
  • 06-17-2009 12:47 PM

    • afshin
    • Not Ranked
    • Joined on 06-17-2009
    • Posts 4

    Readin Blob data in C#

    Hi,

     I'm using the latest Wrapper, Installed about a week ago.

     I try to use SQLiteDataReader to read a file from the database and write it to a file, but the problem is I can't read it, I either get Cast Exception or No Current Row.

    For No Current row, if i use reader.Read(); Then I get CAST Exception.

    can any one help please?  

    OR has anyone done this before, and willing to share a sample code?

     Any elp is greatly appreciated,

     Thanks,


     

    Filed under: , , ,
  • 06-18-2009 10:56 PM In reply to

    • afshin
    • Not Ranked
    • Joined on 06-17-2009
    • Posts 4

    Re: Readin Blob data in C#

     Thanks to Joel Lucsy  It has been fixed.

    Here is his code:

    public T GetValue<T>( String query )
    {
    using (IDbCommand cmd = connection.CreateCommand())
    {
    cmd.CommandText = query;
    return (T)cmd.ExecuteScalar();
    }
    }
    public void foo()
    {
    byte[ img = GetValue<byte[>( "select imageblob from imagetable" );
    }

     I hope this helps those with similar problem.

    Filed under: , , ,
  • 06-19-2009 8:48 AM In reply to

    Re: Readin Blob data in C#

    The GetBytes(...) or GetValue() methods on SQLiteDataReader also works for this general purpose, esp. if you want to read multiple rows.  GetValue() is what ExecuteScalar is calling internally.

  • 06-19-2009 9:52 AM In reply to

    • afshin
    • Not Ranked
    • Joined on 06-17-2009
    • Posts 4

    Re: Readin Blob data in C#

     But consider this line:

    long len = rdr.GetBytes(1, 0, null, 0, 0);

     it throws a Cast Exception,

     Check: http://sqlite.phxsoftware.com/forums/t/1781.aspx

     Any suggestion there?

     

    Filed under: , , ,
  • 06-19-2009 10:21 AM In reply to

    Re: Readin Blob data in C#

    Just tried the GetBytes call with a null array to fetch the length on one of my databases, and it works fine, no cast exception.

    Note that the first argument is the column number, if you get that wrong, then it's logical that you may get an InvalidCastException.  In your sample query, you need column 0, not column 1 as it is in the code you pasted.

    I'm not sure what's going on on in your other post.  I'd guess at first that the column thought to contain a blob instead contained text, except that the GetFieldType indicated that it was indeed a blob.  The stack trace from the InvalidCastException would help answer that question.

Page 1 of 1 (5 items)
Powered by Community Server (Commercial Edition), by Telligent Systems