简体   繁体   English

如何在单击按钮时从datagridview中的sqlserver检索所有类型的数据?

[英]How to retrive all types of data from sqlserver in datagridview on button click?

I want to know how I can retrieve image's from sqlserver in DataGridView on a button click.我想知道如何通过单击按钮从 DataGridView 中的 sqlserver 检索图像。 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#.那么如何在按钮单击时在 DataGridView 中按列显示所有类型的数据,包括 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#. SQL 数据类型image在 c# 中作为byte[]返回。 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM