C# - How To Insert Image To MySQL Using Stored Procedure In C#
__________________________________________________________________________
In This C# Code We Will See How Add A Picture To MySQL database
In CSharp Programming Language .
In This C# Code We Will See How Add A Picture To MySQL database
In CSharp Programming Language .
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.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.IO;
namespace Csharp_And_MySQL
{
public partial class Csharp_insert_image_into_mysql_database : Form
{
MySqlConnection connection = new MySqlConnection("datasource=localhost;port=3306;username=root;password=");
MySqlCommand command;
public Csharp_insert_image_into_mysql_database()
{
InitializeComponent();
}
private void BTN_BROWSE_Click(object sender, EventArgs e)
{
OpenFileDialog opf = new OpenFileDialog();
opf.Filter = "Choose Image(*.jpg; *.png; *.gif)|*.jpg; *.png; *.gif";
if (opf.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = Image.FromFile(opf.FileName);
}
}
private void BTN_INSERT_IMAGE_Click(object sender, EventArgs e)
{
MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms,pictureBox1.Image.RawFormat);
byte[] img = ms.ToArray();
String insertQuery = "INSERT INTO db_images.myimages(ID, Name, Description, Image) VALUES(@id, @name, @desc, @img)";
connection.Open();
command = new MySqlCommand(insertQuery, connection);
command.Parameters.Add("@id", MySqlDbType.VarChar,20);
command.Parameters.Add("@name", MySqlDbType.VarChar, 200);
command.Parameters.Add("@desc", MySqlDbType.Text);
command.Parameters.Add("@img", MySqlDbType.Blob);
command.Parameters["@id"].Value = textBoxID.Text;
command.Parameters["@name"].Value = textBoxNAME.Text;
command.Parameters["@desc"].Value = textBoxDESC.Text;
command.Parameters["@img"].Value = img;
if(command.ExecuteNonQuery() == 1)
{
MessageBox.Show("Data Inserted");
}
connection.Close();
}
}
}
=> OUTPUT :
using Stored Procedure 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 System.IO;
using MySql.Data.MySqlClient;
namespace WindowsFormsApplication1 {
public partial class Form2 : Form {
public Form2() { InitializeComponent(); } MySqlConnection connection = new MySqlConnection("datasource=localhost;port=3306;Initial Catalog='DB_Images';username=root;password=");
MySqlCommand command;
private void button1_Click(object sender, EventArgs e) { MemoryStream ms = new MemoryStream(); pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat); byte[] img = ms.ToArray();
MySqlParameter[] parameters = new MySqlParameter[4]; parameters[0] = new MySqlParameter("ID",MySqlDbType.VarChar,20); parameters[0].Value = textBoxID.Text; parameters[1] = new MySqlParameter("Name", MySqlDbType.VarChar, 200); parameters[1].Value = textBoxNAME.Text; parameters[2] = new MySqlParameter("Description", MySqlDbType.Text); parameters[2].Value = textBoxDescription.Text; parameters[3] = new MySqlParameter("Image", MySqlDbType.LongBlob); parameters[3].Value = img;
command = new MySqlCommand(); command.CommandType = CommandType.StoredProcedure; command.CommandText = "Add_Data";
command.Connection = connection;
command.Parameters.AddRange(parameters);
connection.Open();
command.ExecuteNonQuery();
MessageBox.Show("OK");
connection.Close(); }
//browse image in pictureBox private void button2_Click(object sender, EventArgs e) { OpenFileDialog opf = new OpenFileDialog(); opf.Filter = "Choose Image(*.jpg; *.png; *.gif)|*.jpg; *.png; *.gif"; if (opf.ShowDialog() == DialogResult.OK) { pictureBox1.Image = Image.FromFile(opf.FileName); } } } }///////////////////// END MySQL Stored Procedure To Insert The Image DELIMITER // CREATE PROCEDURE Add_Data(IN ID varchar(20),IN Name varchar(200),IN Description text,IN Image longblob) BEGIN insert into myimages(ID,Name,Description,Image) values (ID,Name,Description,Image); END // DELIMITER ; ///////////////OUTPUT:
Download Projects Source Code
1 comments:
commentsThank you!
Reply