简体   繁体   English

如何在C#中将图像从SQL Server 2008 R2检索到Datagridview [Windows应用程序]

[英]How to retrieve image from SQL Server 2008 R2 to Datagridview in C# [Windows Application]

Hi friends i have inserted image to Sql Server and i need to retrieve image from SQL Server 2008 R2 to Datagridview Column in C# [Windows Application].Is there any possible to show the image in Data Grid View is any Respective Size in a column of Grid View. 嗨,朋友们,我已经将图像插入到Sql Server中,并且我需要从SQL Server 2008 R2检索图像到C#[Windows应用程序]中的Datagridview列。是否有可能在Data Grid View中显示该图像,而该图像在网格视图。

My Coding to Insert the images to SQL Server is : 我将图像插入SQL Server的代码是:

  public void saveImageInDataBase(int imageid)
    {

        byte[] imagedata = ReadImageFile(txt_upload.Text);
        SqlConnection sqlconn = new SqlConnection();
        sqlconn.ConnectionString = "Data Source=.;Initial Catalog=db_payroll;Integrated Security=True;";
        sqlconn.Open();
        string query = "insert into tbl_image values('"+imagedata+"')";
        MessageBox.Show(query);
        SqlCommand cmd = new SqlCommand(query, sqlconn);
        int rows = cmd.ExecuteNonQuery();
        if (rows > 0)
        {
            MessageBox.Show("Image saved.");
            txt_upload.Text = "";
            pictureBox1.Image = null;
        }
        else
        {
            MessageBox.Show("Unable to save image.");
            sqlconn.Close();
        }
    }

    public byte[] ReadImageFile(string imageLocation)
    {
        byte[] imageData = null;
        FileInfo fileInfo = new FileInfo(imageLocation);
        long imageFileLength = fileInfo.Length;
        FileStream fs = new FileStream(imageLocation, FileMode.Open, FileAccess.Read);
        BinaryReader br = new BinaryReader(fs);
        imageData = br.ReadBytes((int)imageFileLength);
        return imageData;
    }

Please try to Solve this issue. 请尝试解决此问题。 Thanks 谢谢

Store to DB (in DB the imagedata column must have IMAGE data type) 存储到数据库(在数据库中, imagedata列必须具有IMAGE数据类型)

    string query = "insert into tbl_image values(@imagedata)";
    SqlCommand cmd = new SqlCommand(query, sqlconn);
    cmd.Parameters.Add("@imagedata", System.Data.SqlDbType.Image);
    cmd.Parameters["@imagedata"].Value = imagedata;
    int rows = cmd.ExecuteNonQuery();

To retrieve from DB 从数据库检索

    string query = "select imagedata from tbl_image where ID=@ID";
    SqlCommand cmd = new SqlCommand(query, sqlconn);
    cmd.Parameters.AddWithValue("@id", id);
    byte[] imagedata=(byte[])cmdSelect.ExecuteScalar();

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

相关问题 如何通过网络从C#ClickOnce应用程序访问SQL Server 2008 R2 - How to access sql server 2008 R2 from C# ClickOnce application over a network 如何将数据从datagridview保存到SQL Server 2008 R2 - How to save data from datagridview to SQL Server 2008 R2 SQL Server 2008 R2数据库备份和还原功能,通过C#Windows应用程序 - Sql Server 2008 R2 Database backup and restore functionality by C# Windows Application 在C#Windows应用程序中使用MS SQL SERVER 2008 R2 EXPRESS? - Use of MS SQL SERVER 2008 R2 EXPRESS with C# Windows Application? 从C#控制台应用程序关闭Windows Server 2008 R2计算机 - Shut down windows server 2008 r2 machine from c# console application 如何使用C#仅从SQL Server 2008 R2备份表数据 - How to backup only table data from SQL Server 2008 R2 using C# 当我从sql server 2008 r2检索图像时,图像为黑色 - When I retrieve image from sql server 2008 r2 the image is black 如何使用Windows Service(C#)中的多线程在SQL Server 2008 R2中插入和更新同一插入的多条记录? - How to insert & update the same inserted multiple record in SQL Server 2008 R2 using multithread in windows service(C#)? 部署使用Asp.Net,C#和Sql Server 2008 R2构建的Web应用程序 - Deploying an Web Application built using Asp.Net,C# and Sql Server 2008 R2 使用C#安装SQL Server 2008 R2 Express - Install SQL Server 2008 R2 Express using C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM