View Full Version : مبتدی: چگونگی ذخیره عکس در sql

جمعه 26 آذر 1389, 14:37 عصر
لطفا راهنماییم کنین چطوری در sql عکس رو ذخیره کنم و در c# هم چطور از اون استفاده کنم یا تو جدولم عکسی ذخیره کنم؟؟؟؟؟؟؟

جمعه 26 آذر 1389, 18:26 عصر
این سوال بار ها پرسیده شده

اینجا (http://barnamenevis.org/showthread.php?263902-%D8%B0%D8%AE%DB%8C%D8%B1%D9%87-%D8%B9%DA%A9%D8%B3-%D8%AF%D8%B1-%D8%AF%DB%8C%D8%AA%D8%A7%D8%A8%DB%8C%D8%B3-%28-C-2010-%29)رو ببینید

جمعه 15 بهمن 1389, 17:28 عصر
سلام دوستان عزیز این اولی راه حلی در سایت کد پروجکت :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

//author:Morteza Naeimabadi Location=IRAN ,Yazd
//written in 2008/03/12

namespace SampleOfMorteza
public partial class Form1 : Form
public Form1()

private void button1_Click(object sender, EventArgs e)
//We are using SQL express.
//My database name is "PictureDb".
SqlConnection con = new SqlConnection
("Server=.;database=PictureDb;integrated security=true");
//I have used a table named "tblUsers" and fill the fields
SqlCommand com = new SqlCommand("insert into tblUsers
(fldCode,fldPic) values(1,@Pic)", con);

//In here, I have to save the picturebox image to tblUsers
//because you were not able to send the PictureBox1.Image
//to field "fldPic" that is of kind
//"image" in SQL SERVER EXPRESS, then you should do something else
// and that is converting
//your picture box image to an array of bytes and then sending
//that array to database.
//Below, we have used a stream which will be used to
//contain our picturebox image bytes.
MemoryStream stream=new MemoryStream();
//through the instruction below, we save the
//image to byte in the object "stream".
pictureBox1.Image.Save(stream,System.Drawing.Imagi ng.ImageFormat.Jpeg);

//Below is the most important part, actually you are
//transferring the bytes of the array
//to the pic which is also of kind byte[]
byte[] pic=stream.ToArray();

com.Parameters.AddWithValue("@Pic", pic);
catch (Exception ex)

private void button2_Click(object sender, EventArgs e)
SqlConnection connect = new SqlConnection
("Server=.;database=PictureDb;integrated security=true");
SqlCommand command = new SqlCommand
("select fldPic from tblUsers where fldCode=1", connect);
//for retrieving the image field in SQL SERVER EXPRESS
//Database you should first bring
//that image in DataList or DataTable
//then add the content to the byte[] array.
//That's ALL!
SqlDataAdapter dp = new SqlDataAdapter(command);
DataSet ds = new DataSet("MyImages");

byte[] MyData = new byte[0];

dp.Fill(ds, "MyImages");
DataRow myRow;
myRow = ds.Tables["MyImages"].Rows[0];

MyData = (byte[])myRow["fldPic"];

MemoryStream stream = new MemoryStream(MyData);
//With the code below, you are in fact converting the byte array of image
//to the real image.
pictureBox2.Image = Image.FromStream(stream);

این یکی کاری که خودم انجام دادم فقط به یاد داشته باشید که باید هدر زیرا داشته باشید
usuing syste,.io

MemoryStream Mspic = newMemoryStream();
pictureBox1.Image.Save(Mspic, pictureBox1.Image.RawFormat);
byte[] pic = Mspic.ToArray();
Mycommand.CommandText = "INSERT INTO dbo.Personattributes(image)" +
"VALUES (Image);";
Mycommand.Parameters.Add("@Image", SqlDbType.Image).Value = pic;