Insert and retrive images from SQL Server

Add to Favourites
Post to:

How can I insert and retrieve images in SQL Server?? Say, we have a table Images with following attributes: PId char(5) Picture image We can now use the store procedures SPInImage and SPSelImage to insert and retrieve images from the database. CREATE PROCEDURE SPInImage ( @PId char(5), @Picture image ) AS IF NOT EXISTS(SELECT * From Images WHERE PId=@PId) BEGIN INSERT INTO Images (PId, Picture ) VALUES ( @PId, @Picture ) END GO CREATE PROCEDURE SPSelImage ( @PId char(15) ) AS SELECT Picture FROM Images WHERE PId = @PId GO Figure 23 Now, we can use the methods bool InsertImage(string strPId,byte[] imPicture) and byte[] GetImage(string strPId) to invoke these store procedures(SeeFigure 24) ///

///Inserts a picture to the table /// /// /// /// publicbool InsertImage(string strPId,byte[] imPicture) { bool bInsert = false; try { //lock the intrinsic DataAccess utilities Monitor.Enter(this); //Initialise DataAccess utilities for the local server this.Prepair_L("SPInImage"); //Add the parameters to SqlCommand SqlParameter pmPId = pmFactory_In.GetPMChar5("@PId"); pmPId.Value = strPId; this.AddParameter_L(pmPId); SqlParameter pmPicture = pmFactory_In.GetPMImage("@Picture"); pmPicture.Value = imPicture; this.AddParameter_L(pmPicture); //Excequte the query this.Open_L(); int nExcequte = ExcecuteNonQuery_L(); if(nExcequte>0) { bInsert = true; } } catch(Exception oException) { string strError = "An Error Occured in :InsertImage"; ErrorLog(strError,oException); } finally { this.Close_L(); Monitor.Exit(this); } return bInsert; } /// ///retrieves an image from the table 􁪽images􁪽////// publicbyte[] GetImage(string strPId) { byte[] imPicture = null; try { //lock the intrinsic DataAccess utilities Monitor.Enter(this); //Initialise DataAccess utilities for the local server this.Prepair_L("SPSelImage"); //Add the parameters to SqlCommand SqlParameter pmPId = pmFactory_In.GetPMChar15("@PId"); pmPId.Value = strPId; this.AddParameter_L(pmPId); //Excequte the query this.Open_L(); object oScalar = this.ExcecuteScalar_L(); if(oScalar!=null) { imPicture = (byte[]) oScalar; } } catch(Exception oException) { string strError = "An Error Occured in :GetImage"; ErrorLog(strError,oException); } finally { this.Close_L(); Monitor.Exit(this); } return imPicture; } For personal or group trainings in .NET technologies, SQL Server contact me through my profile: www.wiziq.com/hanuman

Join the .NET Community

Description
This tutorial give compleye explanation on how to insert nd retrive the image from database throug c# coding

Comments

Want to learn?

Sign up and browse through relevant courses.

Name:
Your Email:
Password:
Country:
Contact no:


Area code Number
Subjects you are interested in:
Word verification: (Enter the text as in image)


Sign Up Already a member? Sign In
I agree to WizIQ's User Agreement & Privacy Policy
ComputerGuroo
Microsoft Certified Trainer(MCT) in .NET, SharePoint, SQL Server BI
User
1 Member Recommends
43 Followers

Your Facebook Friends on WizIQ

Give live classes, create & sell online courses

Try it free Plans & Pricing

Connect