Start New Discussion within our Software Development Community
// writing to access database 

  byte[] buff = null;
   FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);
             long numBytes = fs.Length;
            buff = br.ReadBytes((int)numBytes);
           
             mycon.Open();
           
            OleDbCommand cmd = new OleDbCommand("INSERT INTO song VALUES('1','" + buff + "')", mycon);
            cmd.ExecuteNonQuery();

// reading from access database

 OleDbCommand cmd = new OleDbCommand("select file from song", mycon);

           System.Data.OleDb.OleDbDataReader dr;

           dr = cmd.ExecuteReader();
           dr.Read();
           temp = (byte[])dr["file"];

           int len = temp.Length;

although while writing the byte array is of size 970 kb while reading it from the access database and when i try to store it into byte array i get only 25 bytes , solution ?

In building your command ( OleDbCommand cmd = new OleDbCommand("INSERT INTO song VALUES('1','" + buff + "')", mycon); ), you are implicitly converting "buff" (a byte array) to a string, which is not how you handle binary data and even if the cast was able to convert your entire byte array to a string, I'm pretty sure you would exceed the limits of building a command string that way.

I would suggest you use parameterized SQL to build your DbCommand object.

For example:

OleDbCommand cmd = new OleDbCommand("INSERT INTO song (ColumnName) VALUES(@ColumnName)", mycon);

                int len = ((byte[])buff).Length;
                OleDbParameter p = new OleDbParameter("@ColumnName", OleDbType.LongVarBinary, len);
                p.Value = buff;
                cmd.Parameters.Add(p);

NOTE: I don't recall what the max length of the LongVarBinary type is, so you need to be sure you are using the type that corresponds to the DBTYPE needed...

This article has been dead for over six months. Start a new discussion instead.