473,770 Members | 5,977 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how do I save images to SQL DB?

I'm having the wierdest problem. I'm trying to save images into a SQL 2005
database. the field is just called "IMAGES" and hte data type is "image".
here is my code to save

//following code saves an image as a byte array to a DB

string strFn = this.openFileDi alog1.FileName;
this.pictureBox 1.Image = Image.FromFile( strFn);
FileInfo fiImage = new FileInfo(strFn) ;
m_lImageFileLen gth = fiImage.Length;
FileStream fs = new FileStream(strF n, FileMode.Open, FileAccess.Read ,
FileShare.Read) ;
fs.Seek(0, SeekOrigin.Begi n);
m_barrImg = new byte[Convert.ToInt32 (m_lImageFileLe ngth)];
int iBytesRead = fs.Read(m_barrI mg, 0, Convert.ToInt32 (m_lImageFileLe ngth));
fs.Close();

//method takes in customerID, and byte array of image data
savepicture(Cus tomerID, m_barrImg

//the following code pulls it back out
//first get the byte array back from SQL using this method.
Byte[] barrImg = DBAccess.getima ge(CustomerID);

string strfn = Convert.ToStrin g(DateTime.Now. ToFileTime());
FileStream fs1 = new FileStream(strf n, FileMode.Create New, FileAccess.Writ e);
fs1.Write(barrI mg, 0, barrImg.Length) ;
fs1.Flush();
fs1.Close();

//try to display it
pictureBox1.Ima ge = Image.FromFile( strfn);

my proble is that when I get it BACK, the byte array size is only 13! when I
put it in, it's much larger than that.

why is it always 13 ? what am I doing wrong? the methods for update and
select are basic UPDATE TABLE SET IMAGE = ' m_BarrImg ' WHERE CUSTOMERID =
(whatever) , and the SELECT is the same simplicity SELECT IMAGE WHERE
CUSTOMERID = (whatever).

why is this happening? I have tried to a couple hours now. thanks.

PS. NO stored procedures thanks. :)
Aug 3 '07 #1
3 4399
Rogelio wrote:
I'm having the wierdest problem. I'm trying to save images into a SQL 2005
database. the field is just called "IMAGES" and hte data type is "image".
here is my code to save
//the following code pulls it back out
//first get the byte array back from SQL using this method.
Byte[] barrImg = DBAccess.getima ge(CustomerID);
my proble is that when I get it BACK, the byte array size is only 13! when I
put it in, it's much larger than that.

why is it always 13 ? what am I doing wrong? the methods for update and
select are basic UPDATE TABLE SET IMAGE = ' m_BarrImg ' WHERE CUSTOMERID =
(whatever) , and the SELECT is the same simplicity SELECT IMAGE WHERE
CUSTOMERID = (whatever).
How does the code in DBAccess.getima ge look like ?

Arne
Aug 3 '07 #2
On Sat, 04 Aug 2007 01:06:02 +0200, Rogelio <Ro*****@discus sions.microsoft .comwrote:
I'm having the wierdest problem. I'm trying to save images into a SQL 2005
database. the field is just called "IMAGES" and hte data type is "image".
here is my code to save

//following code saves an image as a byte array to a DB

string strFn = this.openFileDi alog1.FileName;
this.pictureBox 1.Image = Image.FromFile( strFn);
FileInfo fiImage = new FileInfo(strFn) ;
m_lImageFileLen gth = fiImage.Length;
FileStream fs = new FileStream(strF n, FileMode.Open, FileAccess.Read ,
FileShare.Read) ;
fs.Seek(0, SeekOrigin.Begi n);
m_barrImg = new byte[Convert.ToInt32 (m_lImageFileLe ngth)];
int iBytesRead = fs.Read(m_barrI mg, 0, Convert.ToInt32 (m_lImageFileLe ngth));
fs.Close();

//method takes in customerID, and byte array of image data
savepicture(Cus tomerID, m_barrImg

//the following code pulls it back out
//first get the byte array back from SQL using this method.
Byte[] barrImg = DBAccess.getima ge(CustomerID);

string strfn = Convert.ToStrin g(DateTime.Now. ToFileTime());
FileStream fs1 = new FileStream(strf n, FileMode.Create New, FileAccess.Writ e);
fs1.Write(barrI mg, 0, barrImg.Length) ;
fs1.Flush();
fs1.Close();

//try to display it
pictureBox1.Ima ge = Image.FromFile( strfn);

my proble is that when I get it BACK, the byte array size is only 13! when I
put it in, it's much larger than that.

why is it always 13 ? what am I doing wrong? the methods for update and
select are basic UPDATE TABLE SET IMAGE = ' m_BarrImg ' WHERE CUSTOMERID =
(whatever) , and the SELECT is the same simplicity SELECT IMAGE WHERE
CUSTOMERID = (whatever).

why is this happening? I have tried to a couple hours now. thanks.

PS. NO stored procedures thanks. :)
I don't see anything particularly wrong with your code (that my sleepy eyes registeres at 1:38), although you do seem overly fond of Convert, which isn't necessary. You should also enclose your streams in using statements, and use SqlParameters in your save/read database methods.

It might be worth rewriting it IMAGE to [IMAGE] (tells the SQL server that you are talking about the IMAGE field and not the IMAGE data type

string strFn = this.openFileDi alog1.FileName;
FileInfo fiImage = new FileInfo(strFn) ;

using (FileStream fs = File.OpenRead(s trFn))
{
byte[] data = new byte[fiImage.Length];
int iBytesRead = fs.Read(data, 0, fiImage.Length) ;

savepicture(Cus tomerID, data);
}

string strfn = DateTime.Now.To FileTime().ToSt ring());
using (FileStream fs1 = File.Create(str fn))
{
byte[] barrImg = DBAccess.getima ge(CustomerID);
fs1.Write(barrI mg, 0, barrImg.Length) ;
}

If you post your save and read methods I'll take a look at them tomorrow(that is, later today) unless you get your answers before then.

--
Happy coding!
Morten Wennevik [C# MVP]
Aug 3 '07 #3
I figured it out. after about 5 hours of searching and searching. the problem
is my UPDATE statement. I cant use

UPDATE TABLE SET IMAGE = ' "+ m_BarrImg + " ' WHERE CUSTOMERID = (whatever)

I have to make a SQLDBType.Image object. like this...

SqlConnection SqlCon = new SqlConnection(C onnectionStr);

string sql = "UPDATE CUSTOMER SET IMAGE = (@ImagPar)";

SqlCommand cmd = new SqlCommand();
SqlParameter sp = new SqlParameter("@ ImagPar",SqlDbT ype.Image);
sp.Value = m_barrImg;
SqlCmd.Paramete rs.Add(sp);
SqlCmd.CommandT ext = sql;
SqlCmd.CommandT ype = CommandType.Tex t;
SqlCmd.Connecti on = con;

//open connection
con.Open();

//execute the sql command
cmd.ExecuteNonQ uery();

con.Close();

etc...

I cant believe I searched for so long only to find the answer minutes after
I finally decided to ask. O well. thanks.
"Arne Vajhøj" wrote:
Rogelio wrote:
I'm having the wierdest problem. I'm trying to save images into a SQL 2005
database. the field is just called "IMAGES" and hte data type is "image".
here is my code to save
//the following code pulls it back out
//first get the byte array back from SQL using this method.
Byte[] barrImg = DBAccess.getima ge(CustomerID);
my proble is that when I get it BACK, the byte array size is only 13! when I
put it in, it's much larger than that.

why is it always 13 ? what am I doing wrong? the methods for update and
select are basic UPDATE TABLE SET IMAGE = ' m_BarrImg ' WHERE CUSTOMERID =
(whatever) , and the SELECT is the same simplicity SELECT IMAGE WHERE
CUSTOMERID = (whatever).

How does the code in DBAccess.getima ge look like ?

Arne
Aug 3 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2904
by: GMane Python | last post by:
Hello All. I have a program that downloads 'gigabytes' of Axis NetCam photos per day. Right now, I set up the process to put the images into a queue, and every 30 or so seconds, 'pop' them from the queue and save them to disc. I save them as individual files. I think that I'd like to modify it to save into one file 100-200 images, so that I don't have directories with 50,000-90,000 frames before handing that off to a DivX Encoder.
9
2644
by: Mark Johnson | last post by:
How can you save all or a portion of the Grafics object to a Image/Bitmap ? I am try to save the Images from Cards.dll to a BitMap file. I can read in the Images to the Grafics, but when I try this with a Bitmap the results are Black. Can you somehow Clip the Grafic and Paste it into the Bitmap ? Mark Johnson, Berlin Germany mj10777@mj10777.de
9
3030
by: Ivan Demkovitch | last post by:
Hi! I would like to know if I can save File on Server using server-side code? For example, I like to create thumbnail images and populate specific directory. Do I need specific permissions to do this? (I use public host) Thanks!
6
2503
by: Mike | last post by:
can i open the save file dialog box from a asp.net web page? thx
7
3808
by: Stan Sainte-Rose | last post by:
Hi, How can I save a page loaded with AxWebBrowser into a winform as an htm page ? Stan
1
5518
by: mohan21_kumar | last post by:
Hi, How to Save images in ms word file when it is downloaded from asp.net application. I have used the following code to convert the html page into ms word file. but i'm not able to save the images from html to doc file. Please suggest me how to save an image from a html to doc file. I have used the following Code to convert HTML into doc file: Response.Clear(); Response.Buffer = true; Response.ContentType =...
1
6826
by: liuliuliu | last post by:
hi -- sorry if this is trivial -- but how do you make a screenshot of a pygame display? i have a surface which is basically the entire visible screen -- how do you write this surface as an image file during specific events in the script execution? image format doesnt matter. thanks! christine
12
4777
by: =?Utf-8?B?RnJlZU5FYXN5?= | last post by:
Hello, the scenario: There's an ASPX page which shows some text and has three buttons at the bottom: Save, Print and Close. Print and close is done by javascript. But how can I save the page on the client's computer? He/she could do this using the browser (file/save), but I need to have it done by pressing the pushbutton. In my serverside code I get the button-click-event, I also know how to get
3
3684
by: Angus | last post by:
I have a web page with a toolbar containing a Save button. The Save button can change contextually to be a Search button in some cases. Hence the button name searchsavechanges. The snippet of html is: <a class="searchsavechanges btn btn3d tbbtn" href="javascript:" style="position:static"> <div id="TBsearchsavechanges">Search</div> </a>
2
3022
by: Simon Wigzell | last post by:
I have inherited a database driven website that comes with a table of image links. The images are scattered all of the internet and there are thousands of them. I would like to write an asp script to just run through the table and download every link by it's URL. I thought I had id by faking a form with the "File" element and loading it with all the URLs. Would have worked except you cannot preload the "File" element. As I understand it,...
0
9592
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
9425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10231
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...
0
10059
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10005
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
8887
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
6679
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5452
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3972
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

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.