dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online Membersvijaya
Phagu Mahato
More...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Resources » Code Snippets » C# Syntax

How to Insert Image into SQL Server using .Net


Posted Date:     Category: C# Syntax    
Author: Member Level: Gold    Points: 40


In this article, I will explain step by step how to store images in a SQL Server database.



 


This article show you how to store image in database step by step.

A) Query to create table


CREATE TABLE [TestTable](
[ID] [bigint] NOT NULL,
[testImage] [image] NULL,)


Here testImage to store image and defined as image datatype. This field store
image in binary format and enough to store any image.

B) Create procedure to insert image into table



CREATE PROCEDURE [sp_InsertImage]
(
@ID bigint,
@Image image
)
AS
BEGIN
INSERT INTO [TestTable]
([ID]
,[Image])
VALUES
( @ID, @Image )
END


C)Now Method to insert image.
All descriptions are in the code part.
I am using image file which is stored at C drive.


//Method to insert into Database
private void InsertImage()
{
Try
{
FileInfo fileImage;
fileImage = new FileInfo(@"C:\image1.jpg"); //File image1.jpg to insert into database
long ImageFileLength = fileImage.Length; //Getting file length
byte[] ImageFile = new byte[ImageFileLength];
FileStream fs = new FileStream(@"C:\image1.jpg", FileMode.Open,
FileAccess.Read, FileShare.Read); // open a stream to file
int iBytesRead = fs.Read(@"C:\image1.jpg", 0, Convert.ToInt32(ImageFileLength)); // Read file and store into byte
fs.Close(); // Close the stream

//******************************************************
// I am using enterprise library to connect with database and store information
Database db = DatabaseFactory.CreateDatabase(); \\ Create database instance
DbConnection connection = db.CreateConnection(); \\ Create connection
connection.Open(); // open Connection
DbCommand dbc1;
dbc1 = db.GetStoredProcCommand("SP_InsertImage");
db.AddInParameter(dbc1, "ID", DbType.Int64, 10001);
db.AddInParameter(dbc1, "Image", DbType.Binary, (object) ImageFile);
db.ExecuteNonQuery(dbc1);
if (connection.State == ConnectionState.Open) connection.Close();
//*****************************************************
}
Catch(Exception exc)
{
//Error handling
}
}





Did you like this resource? Share it with your friends and show your love!


Responses to "How to Insert Image into SQL Server using .Net"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: How to set background image in Window Mobile .NET Compact Framework?
    Previous Resource: How to update document property of MS Word file using C#.
    Return to Resources
    Post New Resource
    Category: C# Syntax


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    How to insert image into data base  .  Insert Image into database  .  Image in sql Server  .  Insert image to MS SQL Server  .  Save image into Database  .  



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.