Here is a class for BLOB DB IO which WORKS. I hope some of you would take a close look at the "GetBLOB" method and make sure it is the most efficient. Otherwise Ziad this should be suitable for most of your needs.
(some variable names change to protect the innocent) ------------------------------------------------------------------------------------------ using System; using System.Data.SqlClient; namespace Santra { public class BlobStuff { private System.Data.SqlClient.SqlConnection conn; private System.Data.SqlClient.SqlCommand cmd; private System.Data.SqlClient.SqlDataReader rdr; public override byte[] GetBLOB(string Guid, string type) { System.IO.MemoryStream memStream = new System.IO.MemoryStream(); try { connect(); cmd = conn.CreateCommand(); cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.CommandText = "Get" + type; cmd.Parameters.Add(new SqlParameter("@guid" , System.Data.SqlDbType.VarChar, 50 , System.Data.ParameterDirection.Input, false , 0 , 0, "guid" , System.Data.DataRowVersion.Default , Guid)); rdr = cmd.ExecuteReader(System.Data.CommandBehavior.SequentialAccess); int bufferSize=100; int startIndex=0; byte[] outbyte = new byte[bufferSize]; long retval; rdr.Read(); do { retval = rdr.GetBytes(0, startIndex, outbyte, 0, bufferSize); memStream.Write(outbyte, 0, Convert.ToInt32(retval)); startIndex += bufferSize; }while(retval == bufferSize); disconnect(); } catch(Exception e) { throw new Exception(e.ToString()); } return memStream.ToArray(); } public override System.Boolean SaveBLOB(byte[] blobdata, string guid, string type) { try { connect(); cmd = conn.CreateCommand(); cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.CommandText = "Save" + type; cmd.Parameters.Add(new SqlParameter("@guid" , System.Data.SqlDbType.VarChar, 50 , System.Data.ParameterDirection.Input, false , 0 , 0, "guid" , System.Data.DataRowVersion.Default , guid)); cmd.Parameters.Add(new SqlParameter("@" + type , System.Data.SqlDbType.Image, blobdata.Length , System.Data.ParameterDirection.Input, false , 0 , 0, type , System.Data.DataRowVersion.Default, blobdata)); cmd.ExecuteReader(System.Data.CommandBehavior.SingleRow); disconnect(); } catch(Exception e) { throw new System.Exception(e.ToString()); } return true; } protected override void connect() { conn = new SqlConnection(CONNECTIONSTRING); conn.Open(); } protected override void disconnect() { conn.Close(); } } } At 08:30 AM 06/21/2002 -0700, you wrote: >I haven't worked with the types under System.Data.SqlClient, however when >using the Ole* classes you should be setting the .Size member of the >OleDbParameter variable that represents the image data. So, perhaps you >should add this: > > cmd.Parameters["@blobdata"].Size = imageData.Length; > >HTH, > >Seang > >-----Original Message----- >From: Ziad Sawaf [mailto:[EMAIL PROTECTED]] >Sent: Thursday, June 20, 2002 3:28 PM >To: [EMAIL PROTECTED] >Subject: [ADVANCED-DOTNET] Blobs and ADO.NET > > >I am developing an application that stores images in a database using >ADO.NET. Fetching Blob data seems straightforward using a DataReader. I am >having difficulty inserting BLOBS into the database. During thorough search >I could find only one sample in: > >http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/h >tml/daag.asp > >public void StorePicture( string filename ) >{ > // Read the file into a byte array > FileStream fs = new FileStream( filename, FileMode.Open, >FileAccess.Read ); > byte[] imageData = new Byte[fs.Length]; > fs.Read( imageData, 0, (int)fs.Length ); > fs.Close(); > > SqlConnection conn = new SqlConnection(""); > SqlCommand cmd = new SqlCommand("StorePicture", conn); > cmd.CommandType = CommandType.StoredProcedure; > cmd.Parameters.Add("@filename", filename ); > cmd.Parameters["@filename"].Direction = ParameterDirection.Input; > cmd.Parameters.Add("@blobdata", SqlDbType.Image); > cmd.Parameters["@blobdata"].Direction = ParameterDirection.Input; > // Store the byte array within the image field > cmd.Parameters["@blobdata"].Value = imageData; > try > { > conn.Open(); > cmd.ExecuteNonQuery(); > } > catch > { > throw; > } > finally > { > conn.Close(); > } >} > >This code relies on a stored procedure "StorePicture" that I was not able >to locate. > >Can anyone explain or give a sample how to insert BLOBS in a database? > >Thanks > >Ziad Sawaf > >You can read messages from the Advanced DOTNET archive, unsubscribe from >Advanced DOTNET, or >subscribe to other DevelopMentor lists at http://discuss.develop.com. > >You can read messages from the Advanced DOTNET archive, unsubscribe from >Advanced DOTNET, or >subscribe to other DevelopMentor lists at http://discuss.develop.com. Robert Chartier Author and Developer 604-975-5590 [EMAIL PROTECTED] http://www.aspalliance.com/nothingmn/ You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced DOTNET, or subscribe to other DevelopMentor lists at http://discuss.develop.com.