c# - Retrieve medium blob from MySQL return only 13 bytes -
i searched lot , tried various methods couldn't able solve problem. need save image mysql file.
i used below code save image database.
try { string location = @"c:\users\test\downloads\photos\batman-greenscreen.jpg"; filestream fs = new filestream(location, filemode.open, fileaccess.read); uint32 filelength = (uint32)fs.length; byte[] buffer = new byte[filelength]; fs.read(buffer, 0, (int)filelength); string sqlphotoquery = "insert tab_photo values('" + photo.photoid + "','" + photo.projectid + "','" + photo.day + "','" + photo.barcode + "','" + photo.photoname + "','" + photo.photoxml + "','" + buffer + "','" + filelength + "')"; int result = mysqlhelper.executenonquery(connectionstring, sqlphotoquery); if (result > 0) return true; else return false; } catch (exception e) { return false; }
in file image length 12428,
what tried retrieve data database:
photo photo = new photo(); try { string sqlquery = "select * tab_photo photoid='"+photoid+"'"; mysqldatareader rdr = mysqlhelper.executereader(connectionstring, sqlquery); while (rdr.read()) { photo.photoid = rdr.getstring("photoid"); photo.projectid = rdr.getstring("projectid"); photo.day = rdr.getint32("day"); photo.barcode = rdr.getstring("barcode"); photo.photoname = rdr.getstring("photoname"); photo.photoxml = rdr.getstring("photoxml"); memorystream ms; uint32 filesize; bitmap outimage; int filesize = rdr.getint32(rdr.getordinal("photosize")); byte[] rawdata = new byte[filesize]; rdr.getbytes(rdr.getordinal("photo"), 0, rawdata, 0, (int32)filesize); photo.imagebyte = rawdata; } } catch (exception e) {}
but when retrieve file retrieve first 13 bytes. after value zero , there differences in retrieve values.
i have followed instructions mentioned in handling blob data connector/net.
according have changed settings follows:
this how data saved in database
what reasons can't correct values? there problem in data saving or data retrieving or database configurations?
you cannot have correct data in database. concatenating buffer query string, "system.byte[]" inserted database, since insert statement like
insert tab_photo values(...,'system.byte[]', ...)";
use parameterized queries in example, , should good.
do not concatenate strings somewhere sql queries. (and not catch , swallow exceptions in method this. makes hard debug in case of failure.)
Comments
Post a Comment