Jumat, 10 April 2015

C# - How To Retrieve Image From MySQL Database Using C#

C# - How To Retrieve Image From MySQL Database In C#

__________________________________________________________________________

In This C# Tutorial We Will See How To  Get Picture From MySQL database To PictureBox
 In CSharp Programming Language .


Project Source Code:



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        MySqlConnection connection = new MySqlConnection("datasource=localhost;port=3306;Initial Catalog='DB_Images';username=root;password=");
        MySqlCommand command;
        MySqlDataAdapter da;
        private void buttonShow_Click(object sender, EventArgs e)
        {
            string query = "select * from myimages where ID = '"+textBoxID.Text+"'";
            command = new MySqlCommand(query, connection);
            da = new MySqlDataAdapter(command);
            DataTable dt = new DataTable();
            da.Fill(dt);
            textBoxID.Text = dt.Rows[0][0].ToString();
            textBoxNAME.Text = dt.Rows[0][1].ToString();
            textBoxDescription.Text = dt.Rows[0][2].ToString();
            //set image from mysql database to pictureBox
            byte[] img = (byte[])dt.Rows[0][3];
            MemoryStream ms = new MemoryStream(img);
            pictureBox1.Image = Image.FromStream(ms);
            da.Dispose();
        }
    }
}
/////////////////////  END
///////////////OUTPUT:



Bagikan

Jangan lewatkan

C# - How To Retrieve Image From MySQL Database Using C#
4/ 5
Oleh

Subscribe via email

Suka dengan artikel di atas? Tambahkan email Anda untuk berlangganan.