Dichiara byte[] objData fuori e prima del blocco using, e imposta la dimensione dell'array di byte:
byte []objData=null; using (MemoryStream memStream = new MemoryStream()) { using (FileStream fileStream = File.Open(txtDICOMFilePath.Text, FileMode.Open)) { fileStream.CopyTo(memStream); } int intL = Convert.ToInt32(memStream.Length); objData = new byte[intL]; memStream.Read(objData,0,objData.Length); } SqlCom.Parameters.Add("@ImageData",SqlDb.Image,objData.Length).Value=objData;
MODIFICA: per leggere i dati dalla tabella ImagesStore
string qry = "select * From ImagesStore"; using(SqlConnection Cn=new SqlConnect(CnStr)) { using(SqlCommand SqlCom = new SqlCommand(qry, CN)) { Cn.Open(); using(SqlDataReader dr=SqlCom.ExecuteReader()) { while(dr.Read()) { string path="x:\\folder\\" + dr[0] + ".png"; byte []bytes=(byte[])dr[1]; System.IO.File.WriteAllBytes(path,bytes); } } } }
In alternativa puoi usare DataAdapter/DataTable (il codice è nel post originale)
if (dt.Rows.Count > 0) { foreach(DataRow row in dt.Rows) { string path="x:\\folder\\" + row[0] + ".png"; byte []bytes=(byte[])row[1]; System.IO.File.WriteAllBytes(path,bytes); } }
Nota tecnica
Il tipo SqlDb.Image è deprecato: per i file DICOM, che possono essere voluminosi, conviene dichiarare la colonna come varbinary(max) e il parametro come SqlDbType.VarBinary, così da evitare troncamenti silenziosi sui file più grandi.