C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » C# Syntax »

Inserting image into database & retriving from database.


Posted Date: 12 Dec 2008    Resource Type: Code Snippets    Category: C# Syntax
Author: bulli guruvu settyMember Level: Gold    
Rating: 1 out of 5Points: 5



Hi
Take 2 buttons, 2 picture boxes.
One button is for inserting image into database another for retrieving that image from database to form.

Create table(photo) in SQL server with columns as donor_id,photo.

Code under button1_click:

private void button1_Click(object sender, EventArgs e)
{

SqlConnection con = new SqlConnection("trusted_connection=yes;database=databasename;server=servername");

SqlCommand cmd = new SqlCommand("insert into photo(donor_id,photo) values(1,@Pic)", con);

MemoryStream stream = new MemoryStream();
pictureBox1.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] Pic = stream.ToArray();
cmd.Parameters.AddWithValue("@Pic", Pic);
try
{
con.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("success");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
}

Code for button2_click:

private void button2_Click(object sender, EventArgs e)
{
SqlConnection connect = new SqlConnection("trusted_connection=yes;database=databasename;server=servername");

SqlCommand command = new SqlCommand("select (photo) from photo where donor_id='1'", connect);

SqlDataAdapter dp = new SqlDataAdapter(command);
DataSet ds = new DataSet("MyImages");

byte[] MyData = new byte[0];

dp.Fill(ds, "MyImages");
DataRow myRow;
myRow = ds.Tables["MyImages"].Rows[0];

MyData = (byte[])myRow["photo"];

MemoryStream stream1 = new MemoryStream(MyData);
pictureBox2.Image = Image.FromStream(stream1);
}



Attachments

  • code for inserting & retriving image (23847-12529-Form1.txt)


  • Responses


    No responses found. Be the first to respond and make money from revenue sharing program.

    Feedbacks      
    Popular Tags   What are tags ?   Search Tags  
    Sign In to add tags.
    Inserting & retriving image  .  Image  .  Database  .  

    Post Feedback


    This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
    You must Sign In to post a response.
    Next Resource: Types of Assemblies in C#
    Previous Resource: Textbox validation - Allows Only Letters
    Return to Discussion Resource Index
    Post New Resource
    Category: C# Syntax


    Post resources and earn money!
     
    More Resources



    dotNet Slackers

    About Us    Contact Us    Privacy Policy    Terms Of Use