I want to know how I can retrieve image's from sqlserver in DataGridView on a button click. I can show other types of data with the following code and at image column it is showing error. So how can I show all types of data column-wise in DataGridView on button click including image in c#.
private void ViewButton_Click(object sender, EventArgs e)
{
SqlConnection connection = new SqlConnection("Data Source=DESKTOP-FQBMN3R\\SQLEXPRESS;Initial Catalog=StudentDB; Integrated Security=true");
connection.Open();
SqlDataAdapter sd = new SqlDataAdapter("select * from tbl_student_info", connection);
DataSet ds = new DataSet();
sd.Fill(ds, "tbl_student_info");
ViewDataGridView.DataSource = ds.Tables[0];
connection.Close();
}
The SQL datatype image
is returned as a byte[]
in c#. To turn this into an actual image you'd do something like this:
var imgArray = (byte[])ds.Tables[0].Rows[0]["FILE_IMAGE"];
using (var s = new MemoryStream(imgArray))
{
var img = System.Drawing.Image.FromStream(s);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.