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 .
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 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 //
///////////////OUTPUT:
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 ;
Bagikan
C# - How To Insert Image To MySQL Using Stored Procedure In C#
4/
5
Oleh
insurance