473,765 Members | 2,035 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

19 New Member
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 1375

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

Similar topics

1
2370
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
3048
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 folder on the server and then in the database table that I INSERT the news article, I'll store the path of the uploaded image. To me this seems a bad idea as if the image paths were changed on the
1
1882
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 in database (using SQL server). Currently I am using the following algorithms. Question is can I improve this? 'Get length of stream (lengt of file) in bytes InByteCount = loFile.Length() 'Reallocate storage space for an array variable to...
9
2374
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!!! Cheers,
2
1162
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 from the db it uses an aspx page similar to this <img src="image.aspx?imgid=1" /> I realise there are trade offs between performance security etc, but I think this is going to be a bottleneck in the app going forward. Is there anything I can do to...
2
3121
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 for this ......... or can we do this at database level? Regards, Suresh Chowdary.
2
1213
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
34779
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 sql server handle the embedding of images? Is it better to embed the scaned images in the database or to embed the path and store the images in a folder? My concern is the number of files could eventually reach 7 million and the data would need to be...
0
9568
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10163
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9957
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9835
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8832
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5276
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.