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

  • Thread starter Thread starter Bassem
  • Start date Start date
B

Bassem

Hi all...

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

Thanks,
Bassem.
 
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
http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_
20837398.html

http://forums.aspfree.com/t22808/s12671c1861e4ddb11e294efdf909cb0d.html
http://www.csharpfr.com/gma/listbox+image

http://www.aspmessageboard.com/forum/CSharp.asp?M=666404&P=1
http://www.akadia.com/services/dotnet_read_write_blob.html
hope you find what you need in these examples
 
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;*.JFIF|All files
(*.*)|*.*" ;
ofd.ShowDialog();
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,ParameterDirection.Input,true,0,0,null
, DataRowVersion.Current ,photo);

oleDbConnection1.Open();

Example to add values for Employee and his photo:

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

oleDbConnection1.Close();

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
,oleDbConnection1);
oledbTest.Fill(dsTest ,"EmployeeSalary");
int testrec = dsTest.Tables["EmployeeSalary"].Rows.Count;

if(testrec > 0 )
{
Byte[] byteBLOBData = new Byte[0];
byteBLOBData =
(Byte[])(dsTest.Tables["EmployeeSalary"].Rows[testrec-1]["ImagEmployee"]
);
MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
pBEmpPhoto.Image = Image.FromStream(stmBLOBData);
}


And the exception is:
System.ArpumentException: Invalid parameter user.

Thanks for the help.
Bassem
 
Back
Top