DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C# (http://www.daniweb.com/forums/forum61.html)
-   -   import image to db (http://www.daniweb.com/forums/thread138457.html)

karabela Aug 4th, 2008 1:05 pm
import image to db
 
i created a sql database with table land.
columns;

LandID int PK
LandCodeID int
LandName nvarchar(100)
LandFlag image Allow Nulls

ive created a dataset in my solution.

from my Data Sources i can drag & drop the table Land to my Form1 as DataGridView. it puts all the columns above on my form. im able to update/add/delete Lands except the flag. i want to double click on the landFlagPictureBox so i can select the jpg image to save it in my database. can anyone help me out?

thanx in advance

karabela Aug 6th, 2008 9:41 am
Re: import image to db
 
can anyone give me a good direction. i did a lot of google search. but cant find something usefull.

ramiljoaquin Aug 8th, 2008 12:06 am
Re: import image to db
 
Hey, try this one, I just learn if from http://www.codeproject.com/KB/cs/upl..._DataBase.aspx


Stream streamImage=file1.PostedFile.InputStream;

int imageLenght=file1.PostedFile.ContentLength;

string strType=file1.PostedFile.ContentType;

string strType1=file1.Type;

string strName=txtName.Text;

Byte[] imgData = new byte[imageLenght];

int n = streamImage.Read(imgData,0,imageLenght);

int result = saveImage(strName,imgData,strType);


private int saveImage(string strName,byte[] imgData,string strType)

{ int return1=0;

try

{



SqlCommand cmd= new SqlCommand("INSERT INTO image1(id,imageLenght,imgtype,bytles) values (@id,@imageLenght,@imgtype,@bytles)", con);

cmd.Parameters.Add("@id", SqlDbType.VarChar).Value = strName;

cmd.Parameters.Add("@imageLenght", SqlDbType.BigInt).Value = imgData.Length;

cmd.Parameters.Add("@imgtype", SqlDbType.VarChar,6).Value= strType;

cmd.Parameters.Add("@bytles", SqlDbType.Image).Value = imgData;

con.Open();

return1 = cmd.ExecuteNonQuery();

con.Close();

}

catch(Exception ex)
{
Response.Write(ex.Message);
}



return return 1;

}
finally
{
con.Close();
}

nvmobius Aug 8th, 2008 2:38 pm
Re: import image to db
 
You'll have to trust me on this, but you really DO NOT WANT to store the image in your database. Instead store a file path or Uri to the image in your database and read it from disk when you want to display it.


All times are GMT -4. The time now is 5:51 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC