嗨朋友们,我已将图像插入 Sql Server,我需要将图像从 SQL Server 2008 R2 检索到 C# [Windows 应用程序] 中的 Datagridview 列。是否有可能在 Data Grid View 中显示图像是一列中的任何相应大小网格视图。
我将图像插入 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;
}
请尝试解决此问题。谢谢