المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : image stroing problem from the class property



C# Programming
05-27-2009, 05:31 PM
i am trying to store a image in sqlserver database, but doesnt effect.
help me.


public class Item
{
private int code;
private string name;
private float price;
private Category category;
private byte[] photo;

public Item()
{

}

public Item(int code, string name)
{
this.Code = code;
this.Name = name;
}

public Item(int code, string name, float price) : this(code,name)
{
this.Price = price;
}

public int Code
{
get
{
return this.code;
}
set
{
this.code = value;
}
}

public string Name
{
get
{
return this.name;
}
set
{
this.name = value;
}
}

public float Price
{
get
{
return this.price;
}
set
{
this.price = value;
}
}

public Category Category
{
get
{
return this.category;
}
set
{
this.category = value;
}
}

public byte[] Photo
{
get { return this.photo; }
set { this.photo = value; }
}
}






public class ItemsDAL
{
private string selectItemByCode = "Select * from ItemsWithCategory where code=@code";
private string selectAllItems = "Select * from ItemWithCategory"; // View
private string insertItem = "InsertImage";

public void InserItem(Item pic)
{

SqlConnection con = new SqlConnection(DALHelper.ConnectionString);
SqlCommand cmd = new SqlCommand(this.insertItem,con);

cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = this.insertItem;
cmd.Parameters.Add(new SqlParameter("@photo", pic.Photo));

con.Open();
try
{
cmd.Parameters["@photo"].Value = pic.Photo;
cmd.ExecuteNonQuery();

}
finally
{

con.Close();
}


}
}


public partial class ManageItemsForm : BaseForm
{
Item itm;
ItemsDAL itemDal = new ItemsDAL();

MemoryStream mem = new MemoryStream();
public ManageItemsForm()
{
InitializeComponent();
itm = new Item();


}

private void selectImageToolStripMenuItem_Click(object sender, EventArgs e)
{


DialogResult r = this.openFileDialog1.ShowDialog(this);
if (r == DialogResult.OK)
{

pbxItem.Image = Image.FromFile(openFileDialog1.FileName);
this.pbxItem.BorderStyle = BorderStyle.None;
}
}

private void tabPage1_Click(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
itm.Photo = mem.GetBuffer();
itemDal.InserItem();
this.pbxItem.Image.Save(mem, this.pbxItem.Image.RawFormat);
Dotnet67.Sales.Persons.Customer c = new Dotnet67.Sales.Persons.Customer();
c.Name = txtName.Text;


}

}

Maifs