views:

156

answers:

0

We have the need to create a sqlite database that contains images. The database is created using .net. After the db is created we need to use an air application to pull the images from the sqlite db created with c#. The database is created fine and Air can read all but the BLOB field which contains a bytearray created from an image in .net. My assumption is that the .net bytearray is not compatible with the actionscript bytearray because the actionscript byte array is encoded using the AMF protocol and the .net bytearray is not. Below is the code I use to store the BLOB data. I left out some things for brevity but this is the basic gist.

    using (FileStream stream = new FileStream(f.FullName, FileMode.Open, FileAccess.Read))
    {
          byte[] bytes = new byte[stream.Length];
          stream.Read(bytes, 0, (int)stream.Length);
          stream.Close();
          Add(i,f.Name, bytes);
          Console.WriteLine("Item added ");
    }

    private static void Add(int value,string name,byte[] bytes)
    {
        string txtSQLQuery = "insert into  assets (assetid,filename,media) values (@id,@filename,@media)";
        List<SQLiteParameter> sqlparams = new List<SQLiteParameter>();

        SQLiteParameter id = new SQLiteParameter("@id", DbType.Int32);
        id.Value = value;
        sqlparams.Add(id);

        SQLiteParameter media = new SQLiteParameter("@media", DbType.Binary);
        media.Value = bytes;
        sqlparams.Add(media);

        SQLiteParameter filename = new SQLiteParameter("@filename", DbType.String);
        filename.Value = name;
        sqlparams.Add(filename);

        SetConnection();
        sql_con.Open();
        sql_cmd = sql_con.CreateCommand();
        sql_cmd.CommandText = txtQuery;
        sql_cmd.Parameters.AddRange(sqlparams.ToArray());
        sql_cmd.ExecuteNonQuery();
        sql_cmd.Dispose();
        sql_con.Close();
    }