开发者

Put and retrivel image in SQLite database

please help. I read here some topics, but don't get answer on my question. I try put image in BLOB type of SQLite DB and then retrive it, but it failed.

So, using rawDoc = File.ReadAllBytes(openFileDialog.FileName); i get byte[] rawDoc of some image (or other file). Then

String dbConnection = "Data Source=dbase.s3db";
SQLiteConnection cnn = new SQLiteConnection(dbConnection);
cnn.Open();
String sql = String.Format("INSERT INTO registry" +
            "(NameDoc, YearDoc, MonthDoc, DayDoc, RawDoc) VALUES" +
            "('{0}', '{1}', '{2}', '{3}', '{4}')",
            txtNameDoc.Text, numUDYear.Value, numUDD.Value, numUDD.Value, Convert.ToBase64String(rawDoc));

if i try verify rawDoc with that

byte[] imageBytes = Con开发者_Go百科vert.FromBase64String(testRaw);
        MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
        ms.Write(imageBytes, 0, imageBytes.Length);
        pictureBox1.Image = Image.FromStream(ms, true);

I get normal picture. But if i try do that with data from SQLite base with

SQLiteCommand myCommand = new SQLiteCommand(query, cnn);
        SQLiteDataReader myDataReader = myCommand.ExecuteReader();

        string raw = null;
        while (myDataReader.Read())
        {
           raw += myDataReader["RawDoc"].ToString().Trim();

        }

        myDataReader.Close();
byte[] imageBytes = Convert.FromBase64String(raw);
        MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
        ms.Write(imageBytes, 0, imageBytes.Length);

        imageBox imgbox = new imageBox();
        PictureBox picBox = new PictureBox();
        picBox.Image = Image.FromStream(ms, true);
        imgbox.Controls.Add(picBox);
        imgbox.Show();

I get an error: FormatException. Invalid characters in a string Base-64. Why?

thanks a lot!


There is no need to convert your byte array to a base 64 string. You'll just end up using more space for nothing in your database. Additionally, you would then actually have TEXT data in your sqlite database, not a BLOB.

What you need to do is used SqliteParameters, for example to insert your BLOB, you can do something like this:

SQLiteCommand command = cnn.CreateCommand();
command.CommandText = String.Format("INSERT INTO registry" +
        "(NameDoc, YearDoc, MonthDoc, DayDoc, RawDoc) VALUES" +
        "('{0}', '{1}', '{2}', '{3}', @0)",
        txtNameDoc.Text, numUDYear.Value, numUDD.Value, numUDD.Value);
SQLiteParameter parameter = new Parameter("@0", System.Data.DbType.Binary);
parameter.Value = rawDoc;
command.Parameters.Add(parameter);
command.ExecuteNonQuery();

When you retrieve the BLOB, get it from your SQliteDataReader this way:

byte[] imageBytes = (System.Byte[])myDataReader["RawDoc"];

EDIT

To answer you're comment, I'm guessing that your stream would need to be re-winded. In Any case, this is I'd do it more this way:

    MemoryStream ms = new MemoryStream(imageBytes);
    PictureBox picBox = new PictureBox();
    picBox.Image = Image.FromStream(ms, true);

EDIT 2

Then I think the problem is with how you retrieve from your DataReader; try something more like this:

    string raw = "";
    while (myDataReader.Read())
       raw = (string)myDataReader["RawDoc"];

This is still probably broken as the loops iterates over all retrieved records, so you'll end up with the image of the last record retrieved.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜