Sunday, May 29, 2005

C# Storing Images- www.funducode.com

image to database

private void insert_Click ( object sender, EventArgs e )
{

FileStream f = new FileStream ( file.Text, FileMode.Open ) ;
byte[ ] buff = new byte [ f.Length ] ;
f.Read ( buff, 0, ( int ) f.Length ) ;
string cmdstr = "Insert into bookinfo values ( @b, @a, @l )" ;
SqlCommand com = new SqlCommand ( cmdstr, con ) ;
com.Parameters.Add ( "@b", book.Text ) ;
com.Parameters.Add ( "@a", author.Text ) ;
com.Parameters.Add ( "@l", buff ) ;
com.ExecuteNonQuery( ) ;
con.Close( ) ;

}



database to image

con.Open( ) ;
r = con.ExecuteReader( ) ;
if ( r.Read( ) )
{

bookl.Text = r [ 0 ].ToString( ) ;
authorl.Text = r [ 1 ].ToString( ) ;
byte[ ] b = ( byte[ ] ) r [ 2 ] ;
MemoryStream st = new MemoryStream( ) ;
st.Write ( b, 0, b.Length ) ;
Image i = Image.FromStream ( st ) ;
img.Image = i ;

}

0 Comments:

Post a Comment

<< Home