Wednesday, March 28, 2012

how to save img in sql db

how to save img in sql db ,I need some one to tell me

Using textcopy.exe is one way to accomplish this. To use ADO.NET code, you might want to use something like this:

int imgLength = UploadControl.PostedFile.ContentLength;

byte[] img = new byte[ imgLength ];

UploadControl.PostedFile.InputStream.Read(
img
, 0
, imgLength);

using( SqlConnection conn = new SqlConnection ( connStringHere ) )
{
using( SqlCommand cmd = new SqlCommand ("dbo.usp_InsertImage", conn) )
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add( "@.img", SqlDbType.Image).Value = img;
conn.Open();
cmd.ExecuteNonQuery();
}
}

This code assumes you have a stored procedure with an 'Image' data type parameter.

Hope This Helps!

No comments:

Post a Comment