Get Image/Picture from Database

Drag and down a PictureBox on the Form.

public partial class Form1 : Form

{

    SqlConnection cnn ;

    string connectionString ;

    public Form1()

    {

         InitializeComponent();

    }

    private void button1_Click(object sender, EventArgs e)

    {

        connectionString = "Data Source=munesh; Initial Catalog=data; User ID=sa; Password=123";

        cnn = new SqlConnection(connectionString);

        MemoryStream stream = new MemoryStream();

        cnn.Open();

        SqlCommand command = new SqlCommand("select img from imgtable where id=2", cnn);

        byte[] image = (byte[])command.ExecuteScalar();

        stream.Write(image, 0, image.Length);

        cnn.Close();

        Bitmap bitmap = new Bitmap(stream);

        pictureBox1.Image = bitmap;

    }

}

Ebook Download
View all
Learn
View all