none
Blob in MySql using c# RRS feed

  • Question

  • Hi I want to storage a txt file into mysql using a blob and after that extract it, the code i use to save it is:
    Code: ( text )
    1. fs = new FileStream("C:\\Documents and Settings\\Pru.txt", FileMode.Open, FileAccess.Read);
    2.             FileSize = fs.Length;
    3.             rawData = new byte[FileSize];
    4.             fs.Read(rawData, 0, Convert.ToInt32(FileSize));
    5.             fs.Close();
    6.             SQL = "INSERT INTO EEG (eeg)values ('" + rawData + "' )";
    7.             cmd.CommandText = SQL;
    8.             cmd.ExecuteNonQuery();

    and my code to extract it is:

    Code: ( text )
    1. MySqlDataReader myData;
    2.     string SQL ="SELECT EEG FROM EEG WHERE id_eeg = '" + id + "' ";
    3.     cmd.CommandText = SQL;
    4.     myData = cmd.ExecuteReader();
    5.     myData.Read();
    6.     rawData = new byte[1000];
    7.     myData.GetBytes(0, 0, rawData, 0,1000);
    8.     for (i = 0; i < rawData.Length;i++)
    9.         final[ i ] = Convert.ToString((rawData[ i ] << 8) + rawData[i+1]);
    10.  fs = new FileStream("C:\\Documents and Settings\\Pru2.out", FileMode.OpenOrCreate,
    11. FileAccess.Write);
    12.     fs.Write(rawData, 0,1000);
    13.     fs.Close();
    14.     myData.Close();
    15.     conn.Close();

    but I get a file with a text like this:
    System.Byte[]

    And it is not the text of the original file
    Help!!!!
    Thx
    Friday, February 8, 2008 12:16 AM

Answers

  • Hi,

     

    I noticed that you use a fixed-length buffer, which is not right way. Instead you should write the exact bytes back to the file with same length as when you write it to database.

     

    There is a varbinary(max) data type in MS SQL Server, and there is a VarBinaryMax.VarBinaryMax sql data type in ADO.NET with which you can store and retrive blob data. I am not familar with MySQL, but if you knew the conterpart you could use it as the type of parameter in the sql command.

     

    Best Regards

    Chunsheng Tang

    Monday, February 11, 2008 9:56 AM