با تشکر از دوستان .راستش من خودم عکسها رو روی سرور و نام آنها را در دیتابیس ذخیره می کنم ولی در این مورد آخر که تنها عکس کاربرم و برای هر نفر یک فیلد دارم می خوام varbinary ذخیره کنم. راستش یک کدی خودم نوشتم که عکس رو به صورت ور باینری ذخیره کرد.حالا می شه کمکم کنید بگید چطور استریمی رو که از دیتا بیس آوردم رو به کنترلimage بایند کنم تا بتونه اونو نمایش بده. کدی هم که نوشتم به صورت زیر است.
string constr = ConfigurationManager.ConnectionStrings["student"].ConnectionString;
protectedvoid Page_Load(object sender, EventArgs e)
{
showphoto();
}
privatevoid AddPhoto(string photoFilePath)
{
byte[] photo = GetPhoto(photoFilePath);
{
SqlConnection objConnection = new SqlConnection(constr);
SqlCommand command = new SqlCommand("INSERT INTO photo (Picture) " + "Values(@Photo)", objConnection);
command.Parameters.Add("@Photo", SqlDbType.VarBinary , photo.Length).Value = photo;
objConnection.Open();
command.ExecuteNonQuery();
}
}
protectedvoid Button1_Click(object sender, EventArgs e)
{
AddPhoto(@"D:\photo1.jpg");
}
privatebyte[] GetPhoto(string filePath)
{
FileStream stream = newFileStream(filePath, FileMode.Open, FileAccess.Read);
BinaryReader reader = newBinaryReader(stream);
byte[] photo = reader.ReadBytes((int)stream.Length);
reader.Close();
stream.Close();
return photo;
}
//////////////////////////////////////////////////////////////////////////////////////
privatevoid showphoto()
{
SqlConnection objConnection = new SqlConnection(constr);
DataSet objDataSet = new DataSet();
SqlDataAdapter objDataAdapter = new SqlDataAdapter();
objDataAdapter.SelectCommand = new SqlCommand();
objDataAdapter.SelectCommand.CommandText = "SELECT Picture FROM photo where id=11";
objDataAdapter.SelectCommand.CommandType = CommandType.Text;
objDataAdapter.SelectCommand.Connection = objConnection;
objDataAdapter.Fill(objDataSet, "photo");
DataView objDataView = new DataView(objDataSet.Tables["photo"]);
objConnection.Open();
byte[] arrPicture = ((byte[])(objDataSet.Tables[0].Rows[0][0]));
//MemoryStream ms = new MemoryStream(arrPicture);
StreamReader rs = newStreamReader(arrPicture);
//از اینجا به بعد دیگه نمی دونم چکار کنم.
}
// }