468,553 Members | 1,304 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,553 developers. It's quick & easy.

Save and retrieve image in SQL Database using Data adapter commands with C#.

Hi all...

I searhed for a code to save and retrieve image from SQL database
using Data adapter but I didn't found anything.

Nov 15 '05 #1
2 11027
Hi Bassem
if you get your data in a dataset using an Using an sqlDataAdapter , then
every thing will be returned as object . if you know that the field you
have in hand is an image then you can cast it to the appropriate type
These links give examples that would help


hope you find what you need in these examples

Nov 15 '05 #2

Thanks Mohamoss....
I read so much in this links and get more ideas.
I implement code to save photo and succeed in that.

The code I Implement in C#. [Using OleDbDataAdapter]

Global field to hold the path of the photo: GSPathImage
The function used to get the path of the photo:
private void EmployeePhoto_Click(object sender, System.EventArgs e)
System.Windows.Forms.OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Employee Photo";
ofd.Filter = "Bitmap (*.BMP;*.DIB;*.)|*.BMP;*.DIB|JPEG
(*.JPG;*.JPEG;*.JPE;*.JFIF)|*.JPG;*.JPEG;*.JPE;*.J FIF|All files
(*.*)|*.*" ;
pBEmpPhoto.Image = new Bitmap(ofd.FileName) ;
GSPathImage = ofd.FileName ;
GSPathImage = GSPathImage == null ? "" : GSPathImage ;
byte[] photo = GSPathImage == "" ? new byte[0]: GetPhoto(GSPathImage) ;

OleDbParameter oledbParam = new OleDbParameter("@ImagEmployee"
,OleDbType.VarBinary,photo.Length,ParameterDirecti on.Input,true,0,0,null
, DataRowVersion.Current ,photo);


Example to add values for Employee and his photo:

oleDbDataAdapter1.InsertCommand.CommandText =
"INSERT INTO EmployeeSalary(EmployeeName , ImagEmployee )"+
"VALUES ('"+lsNewEmpName+"' , '@ImagEmployee' )";


Till now everything going ok and no exception.

When retrieving the photo I get exception.
The code of the retrieving:

string EmployeeValue = txtBEmployeeName.Text.Trim();
string myOleDBstring = "SELECT EmployeeName , ImagEmployee FROM
EmployeeSalary WHERE EmployeeName = '"+ EmployeeValue +"'";
DataSet dsTest = new DataSet();
OleDbDataAdapter oledbTest = new OleDbDataAdapter(myOleDBstring
oledbTest.Fill(dsTest ,"EmployeeSalary");
int testrec = dsTest.Tables["EmployeeSalary"].Rows.Count;

if(testrec > 0 )
Byte[] byteBLOBData = new Byte[0];
byteBLOBData =
MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
pBEmpPhoto.Image = Image.FromStream(stmBLOBData);
And the exception is:
System.ArpumentException: Invalid parameter user.

Thanks for the help.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Sam Martin | last post: by
9 posts views Thread by DapDap | last post: by
3 posts views Thread by =?Utf-8?B?TXJrIEJsYWNrYWxs?= | last post: by
6 posts views Thread by Suresh | last post: by
1 post views Thread by UniDue | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.