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

如何在C#[Windows应用程序]中从SQL Server 2008 R2检索到Datagridview的映像,c#,sql,sql-server-2008,windows-applications,C#,Sql,Sql Server 2008,Windows Applications,嗨,朋友们,我已将图像插入Sql Server,我需要在C#[Windows应用程序]中将图像从Sql Server 2008 R2检索到Datagridview列。是否有可能在数据网格视图中显示图像在网格视图列中的大小 我将图像插入SQL Server的编码为: public void saveImageInDataBase(int imageid) { byte[] imagedata = ReadImageFile(txt_upload.Text);

嗨,朋友们,我已将图像插入Sql Server,我需要在C#[Windows应用程序]中将图像从Sql Server 2008 R2检索到Datagridview列。是否有可能在数据网格视图中显示图像在网格视图列中的大小

我将图像插入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;
    }
请设法解决这个问题。感谢

存储到数据库(在数据库中,
imagedata
列必须具有
IMAGE
数据类型)

从数据库中检索

    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();
存储到数据库(在数据库中,
imagedata
列必须具有
IMAGE
数据类型)

从数据库中检索

    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();

它的作品很好。。。您能告诉我,如何将单元格大小固定为100 X 100,并且图像样式应该是拉伸的。@CoolparRun为此,您可以使用
提问
按钮并创建具有适当标题的新线程。它工作正常。。。您能告诉我,如何将单元格大小固定为100 X 100,并且图像样式应该是拉伸的。@coolprarun为此,您可以使用
Ask Question
按钮并创建具有适当标题的新线程。