473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Storing images in a database using asp.net and c#

19
I am trying to test storing images in a database, I got this code after searching on Google but it doesn't work:

Expand|Select|Wrap|Line Numbers
  1.  protected void Button1_Click(object sender, EventArgs e)
  2.     {
  3.         string strImageName = @"E:\images.jpg";
  4.  
  5.         Bitmap bNewImage = new Bitmap(strImageName);
  6.         FileStream fs = new FileStream(strImageName, FileMode.Open, FileAccess.Read);
  7.         //creating byte array to read image
  8.  
  9.         byte[] bImage = new byte[fs.Length];
  10.  
  11.         //this will store images.jp in bImage byte array
  12.  
  13.         fs.Read(bImage, 0, Convert.ToInt32(fs.Length));
  14.  
  15.         fs.Close();
  16.         fs = null;
  17.  
  18.         //open the database using odp.net and insert the data
  19.  
  20.         string connstr = @"Data Source=PC5-52;Initial Catalog=ImageUpload;User Id=lab;Password=123456;";
  21.         SqlConnection conn = new SqlConnection(connstr);
  22.         conn.Open();
  23.         string strQuery;
  24.  
  25.         strQuery = "INSERT INTO ImageStore (ID, ImageContent) values (" + "1,"  + " @pic)";
  26.  
  27.         SqlParameter ImageParameter= new SqlParameter();
  28.         ImageParameter.SqlDbType = SqlDbType.Image;
  29.         ImageParameter.ParameterName = "pic";
  30.         ImageParameter.Value = bImage;
  31.  
  32.  
  33.  
  34.         SqlCommand cmd = new SqlCommand(strQuery, conn);
  35.         cmd.Parameters.Add(ImageParameter);
  36.         cmd.ExecuteNonQuery();
  37.         Response.Write("Image has been added to database successfully");
  38.  
  39.  
  40.  
  41.         cmd.Dispose();
  42.         conn.Close();
  43.         conn.Dispose();
  44.  
  45.  
  46.     }
On my aspx page I have a simple upload button and my database consists of a simple table with two attributes: Id and ImageContent.

Could anyone please check the above code?
Apr 29 '11 #1
0 1359

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Universal | last post by:
store Images (or any Binary Data) to a SQL Server database using VC++ Can anybody help me in this Thanks in advance
6
by: bissatch | last post by:
Hi, I am currently writing a news admin system. I would like to add the ability to add images to each article. What I have always done in the past is uploaded (using a form) the image to a...
1
by: Al | last post by:
Hi, I need to store literally thousands if images and scanned document in the Database. I would like to know what is the most efficient algorithm both in terms of speed and storage for saving images...
9
by: Adam J Knight | last post by:
Hi all, Just wondering whats everyones prefered method of storing images ? 1) File System 2) Database (SqlServer) (Seems to be easier, but has a performance hit) Appreciate some insight!!!...
2
by: Spondishy | last post by:
Hi, I've taken over a publishing application that stores images in a sql server db. The architecture is basically many load balanced web servers connecting to the db. When an image is pulled...
2
by: suresh Chowdary | last post by:
Hi Friends, i want to store the password in encription format in database .can we any option to do enccription by using java program before storing in database.please give ur valuable suggestions...
2
by: EsahakNoorul | last post by:
Hi, I have stored images in mysql database using blob field. I want to know the background process while displaying on the browser through PHP. Regards, Noorullah
22
MMcCarthy
by: MMcCarthy | last post by:
I have an upcoming project which requires the recording of thumbprint/fingerprint scans in a database (currently proposed as sql server). Now I have a few questions I need to explore: How does...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.