473,387 Members | 1,501 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,387 software developers and data experts.

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.openFileDialog1.FileName;
this.pictureBox1.Image = Image.FromFile(strFn);
FileInfo fiImage = new FileInfo(strFn);
m_lImageFileLength = fiImage.Length;
FileStream fs = new FileStream(strFn, FileMode.Open, FileAccess.Read,
FileShare.Read);
fs.Seek(0, SeekOrigin.Begin);
m_barrImg = new byte[Convert.ToInt32(m_lImageFileLength)];
int iBytesRead = fs.Read(m_barrImg, 0, Convert.ToInt32(m_lImageFileLength));
fs.Close();

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

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

string strfn = Convert.ToString(DateTime.Now.ToFileTime());
FileStream fs1 = new FileStream(strfn, FileMode.CreateNew, FileAccess.Write);
fs1.Write(barrImg, 0, barrImg.Length);
fs1.Flush();
fs1.Close();

//try to display it
pictureBox1.Image = 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 4380
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.getimage(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.getimage look like ?

Arne
Aug 3 '07 #2
On Sat, 04 Aug 2007 01:06:02 +0200, Rogelio <Ro*****@discussions.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.openFileDialog1.FileName;
this.pictureBox1.Image = Image.FromFile(strFn);
FileInfo fiImage = new FileInfo(strFn);
m_lImageFileLength = fiImage.Length;
FileStream fs = new FileStream(strFn, FileMode.Open, FileAccess.Read,
FileShare.Read);
fs.Seek(0, SeekOrigin.Begin);
m_barrImg = new byte[Convert.ToInt32(m_lImageFileLength)];
int iBytesRead = fs.Read(m_barrImg, 0, Convert.ToInt32(m_lImageFileLength));
fs.Close();

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

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

string strfn = Convert.ToString(DateTime.Now.ToFileTime());
FileStream fs1 = new FileStream(strfn, FileMode.CreateNew, FileAccess.Write);
fs1.Write(barrImg, 0, barrImg.Length);
fs1.Flush();
fs1.Close();

//try to display it
pictureBox1.Image = 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.openFileDialog1.FileName;
FileInfo fiImage = new FileInfo(strFn);

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

savepicture(CustomerID, data);
}

string strfn = DateTime.Now.ToFileTime().ToString());
using (FileStream fs1 = File.Create(strfn))
{
byte[] barrImg = DBAccess.getimage(CustomerID);
fs1.Write(barrImg, 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(ConnectionStr);

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

SqlCommand cmd = new SqlCommand();
SqlParameter sp = new SqlParameter("@ImagPar",SqlDbType.Image);
sp.Value = m_barrImg;
SqlCmd.Parameters.Add(sp);
SqlCmd.CommandText = sql;
SqlCmd.CommandType = CommandType.Text;
SqlCmd.Connection = con;

//open connection
con.Open();

//execute the sql command
cmd.ExecuteNonQuery();

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.getimage(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.getimage 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
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...
9
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...
9
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...
6
by: Mike | last post by:
can i open the save file dialog box from a asp.net web page? thx
7
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
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...
1
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...
12
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...
3
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...
2
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...

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.