سلام دوست عزیز...
مثال:
یه فیلد توی جدول tbl ایجاد می کنید با نام pic و نوع varbinary(MAX)...
یه تابع احتیاج داری برای تبدیل فایل به بایت
public byte[] ReadFileToByteArray(string fileName)
{
FileStream fileStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Read);
long len;
len = fileStream.Length;
Byte[] fileAsByte = new Byte[len];
fileStream.Read(fileAsByte, 0, fileAsByte.Length);
MemoryStream memoryStream = new MemoryStream(fileAsByte);
return memoryStream.ToArray();
}
حالا باید عکس تبدیل شده به بایت رو تو دیتابیس ذخیره کنی... می تونی از تابع استفاده کنی
public void FileStoreToDataBase(byte[] content)
{
SqlConnection con =
new SqlConnection("ConnectionStr");
try
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
SqlCommand insert = new SqlCommand("insert into tbl(pic) values (@data)", con);
insert.Parameters.AddWithValue("data", content);
// data.Value = content;
insert.ExecuteNonQuery();
}
finally
{
con.Close();
}
}
برای خواندن اطلاعات و نمایش... این تابع....
public Image showpic()
{
Image image = (Image)null;
try
{
SqlCommand sqlCommand = new SqlCommand("select pic from tbl", con);
if (con.State == ConnectionState.Closed) con.Open();
image = Image.FromStream((Stream)new MemoryStream((byte[])sqlCommand.ExecuteScalar()));
con.Close();
}
catch
{
}
return image;
}