CodeKicks.com
Focus on Microsoft Technologies - Tutorials, Articles, Code Samples.

Wednesday, April 11, 2007

Source Code for Storing Images into SQL Server from .NET

public void StorePicture( string filename )
{
// Read the file into a byte array
using(FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
{
byte[] imageData = new Byte[fs.Length];
fs.Read( imageData, 0, (int)fs.Length );
}

using( SqlConnection conn = new SqlConnection(connectionString) )
{
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;
conn.Open();
cmd.ExecuteNonQuery();
}
}

source code is collected from http://forums.asp.net/thread/1658599.aspx

Post a Comment

Milton Fernando.S said...

Can you explain this in detail..

Thanks

Milton

Microsoft .NET Support Team said...

Please add your email ids for providing the responses.

Milton Fernando.S said...

Email ID:miltonfdo@gmail.com

Thanks

Milton

Shijo said...

Can you explain this to me also?

my mailid is shijokattackayam@gmail.com

Thanks and Regards
Shijo Thomas