PDA

View Full Version : ذخیره تصویر در mysql



Shirin-Balsan
شنبه 26 مرداد 1392, 13:34 عصر
با سلام و خسته نباشید
من می خواستم 1 تصویر رو از برنامم به mysql بفرستم
تا اونجایی که مطالعه کردم بهترین روش اینه که آدرس رو روی db ذخیره کنم و نوعش رو blob انتخاب کنم، متاسفانه کدهایی رو هم که پیدا کردم واسه PHPبود:افسرده: .
اگه کسی لطف کنه روش ذخیره عکس روی کامپیوتر و آدرسش رو روی mysql و همینطور خوندنش رو بگه ممنوم میشم.:خجالت:

arefba
شنبه 26 مرداد 1392, 14:21 عصر
//Creating a filestream to open the image file
FileStream fs = new FileStream(location, FileMode.Open, FileAccess.Read);
//Getting the legth of the fil in bytes
int fileLength = (int)fs.Length;
//creating an array to store the image as bytes
byte[] rawdata = new byte[fileLength];
//using the filestream and converting the image to bits and storing it in //an array
fs.Read(rawdata, 0, (int)fileLength);
//Creating a new mysql command object which will be used to store the //image
MySqlCommand cmd = new MySqlCommand();
//creating sql command
String sql = "insert into savepic values(@size,@file,@name)";
//Setting the connection of the command
cmd.Connection = db.getConnection();
//setting the sql of the command
cmd.CommandText = sql;
//Setting up the parameter values to be used when storing the image to a //table
cmd.Parameters.AddWithValue("@file", rawdata);
cmd.Parameters.AddWithValue("@size", fileLength);
cmd.Parameters.AddWithValue("@name", fileName);
//Executing the query and writing the image to the database
cmd.ExecuteNonQuery();
//Closing the filestream
fs.Close();
MessageBox.Show("Done");

khokhan
شنبه 26 مرداد 1392, 14:25 عصر
با سلام و خسته نباشید
من می خواستم 1 تصویر رو از برنامم به mysql بفرستم
تا اونجایی که مطالعه کردم بهترین روش اینه که آدرس رو روی db ذخیره کنم و نوعش رو blob انتخاب کنم، متاسفانه کدهایی رو هم که پیدا کردم واسه PHPبود:افسرده: .
اگه کسی لطف کنه روش ذخیره عکس روی کامپیوتر و آدرسش رو روی mysql و همینطور خوندنش رو بگه ممنوم میشم.:خجالت:
با سلام
قبل از هر چیز افزودن فضا نام در کلاس فرم :

using MySql.Data.MySqlClient;
using System.IO;
سپس تعریف دو متغیر از نوع رشته یکی برای مسیر فایل و یکی برای نام فایل :


String location;


String fileName;
بعد می رسی به رویداد کلیک دکمه باز کردن فایل تصویر و گرفتن نام و مسیر فایل از کنترل open filedialog :

private void Browse_Click(object sender, EventArgs e)
{
openPic.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";

openPic.ShowDialog();

pictureBox1.BackgroundImage = new Bitmap(openPic.FileName);

location = openPic.FileName;
textBox2.Text = location;

fileName = openPic.SafeFileName;
}
ونهایتا دکمه ثبت اطلاعات :

private void btn_SaveImage_Click(object sender, EventArgs e)
{
MySqlConnection con = new MySqlConnection(ConString);
MySqlCommand cmd;
FileStream fs;
BinaryReader br;
try
{

FileStream fs = new FileStream(location, FileMode.Open, FileAccess.Read);

//گرفتن طول فایل در استریم
int fileLength = (int)fs.Length;

//ایجاد آرای برای ذخیره تصویر به صورت بایت
byte[] rawdata = new byte[fileLength];

//استفاده از استریم برای تبدیل تصویر به بایت

fs.Read(rawdata, 0, (int)fileLength);

//ایجاد کامنت my sql
MySqlCommand cmd = new MySqlCommand();


String sql = "insert into doc1 values(@pfno,@depname,@doctype,@docdesc,@docexpdat e,@docname,@docdisc)";

//رشته اتصال
con = new MySqlConnection();
con.ConnectionString = ConfigurationSettings.AppSettings["constr"];
con.Open();

//اختصاص رشته اتصال به کامنت
cmd = new MySqlCommand(sql, con);



cmd.Parameters.AddWithValue("@name", label8.Text);
cmd.Parameters.AddWithValue("@type", comboBox1.Text);
cmd.Parameters.AddWithValue("@desc", textBox1.Text);
cmd.Parameters.AddWithValue("@docname", textBox2.Text);
cmd.Parameters.AddWithValue("@docdisc", fileLength);
//اجرای کوئری
cmd.ExecuteNonQuery();
//بستن رشته اتصال
con.Close();
MessageBox.Show("ذخیره گردید ");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}