PDA

View Full Version : سوال: نوشتن کلاس ذخیره عکس در sql و نمایش در Grid



mehdin69
دوشنبه 04 بهمن 1389, 01:09 صبح
سلام دوستان
من یه جدول دارم که اطلاعاتی رو توش ذخیره می کنه از جمله عکس کاربر یه توی جدول فیلد تصویر از جنس image هست و کاری که می خوام بکنم اینه که یه کلاس بنویسم برای add اما نمی دونم چطوری برای عکس این کارو کنم
کلاسم اینطوریه حالا باید کجاشو تغییر بدم ؟؟؟


public int ID;
public string Name;
public string Family;
public string Tell;
public string Address;
public string Image;
public string Degree;
public string Birthday;




dataacces da = new dataacces();
public void Add()
{
string sql = @"INSERT INTO member ( ,mname,mfamily,mtell,maddress,mpicture,mdgree,mbir thday
VALUES ({1},N'{2}',N'{3}',N'{4}',N'{5}',{6},N'{7}',N'{8}' )";


string imgFilePath = this.Image;

FileStream fs = File.OpenRead(imgFilePath);
byte[] fileContents = new BinaryReader(fs).ReadBytes((int)fs.Length);


sql = string.Format(sql,
this.ID,
this.Name,
this.Family,
this.Tell,
this.Address,
fileContents,
this,Degree,
this.Birthday);

da.sqlconnect();
da.sqlcommand(sql);
da.sqldisconnect();

}

نکته : کلاس dataaccsess فقط اتصال با دیتابیس رو برقرار می کنه

و حالا روی دکمه ذخیره هم اینو نوشتم:

member mem = new member();
mem.Name = txtname.Text;
mem.Family = txtfamil.Text;
mem.Tell = txttel.Text;
mem.Address = txtaddress.Text;
mem.Degree = txtdegree.Text;
mem.Birthday = txtbdate.Text;
mem.Image = txtimage.Text;
mem.Add();
this.Close();

که در حال حاضر توی خاصیت this.image فقط مسیر عکس ذخیره می شه

حالا من می خوام ببینم چطوری این مسیر عکس رو توی کلاس تبدیل به خود عکس کنم
مرسی :قلب:

navidiran
دوشنبه 04 بهمن 1389, 14:14 عصر
دوست عزیز من نمی دونم شما تکست هات رو باچه روشی در حال insert هستی و کاری هم نداریم. ولی من عکس رو به این روش ثبت می کنم


byte


[] array = ((byte[])conveter.ConvertTo(pictureBox1.Image, typeof(byte[])));

per.Picture = array;

per.Picture = array این هم یعنی : DataRowObj[pictrue]=array;

ااگر در همین سایت جستجو کنی خیلی لینک در این مورد می تونی پیدا کنی
http://barnamenevis.org/showthread.php?243747-روش-ذخیره-عکس-در-داخل-بانک&highlight

mehdin69
دوشنبه 04 بهمن 1389, 18:36 عصر
خب اون چیزی که می خواستم رو توسنستم بنویسم
دوست عزیز مرسی از جوابی که دادید اما یه کم موضوع من فرق داشت...
حالا کدش ر میزام شاید بدرد کسی بخوره
:قلب:


SqlConnection sqlconn = new SqlConnection(appsetting.DBconnstring);
SqlCommand sqlcmd = new SqlCommand("INSERT INTO Member (mname,mfamily,mtell,maddress,mdegree,mpicture)VAL UES(@mname,@mfamily,@mtell,@maddress,@mdegree,@mpi cture)", sqlconn);
sqlcmd.Parameters.AddWithValue("@mname", this.Name);
sqlcmd.Parameters.AddWithValue("@mfamily", this.Family);
sqlcmd.Parameters.AddWithValue("@mtell", this.Tell);
sqlcmd.Parameters.AddWithValue("@maddress", this.Address);
sqlcmd.Parameters.AddWithValue("@mdegree", this.Degree);
// sqlcmd.Parameters.AddWithValue("@birthDate", ShamsiToMiladi(this.Birthday));

string imgFilePath = this.img;

FileStream fs = File.OpenRead(imgFilePath);
byte[] fileContents = new BinaryReader(fs).ReadBytes((int)fs.Length);
sqlcmd.Parameters.AddWithValue("@mpicture", fileContents).SqlDbType = SqlDbType.VarBinary;

sqlcmd.Connection.Open();
sqlcmd.ExecuteNonQuery();
sqlcmd.Connection.Close();