如何在C#中将图像从SQL Server 2008 R2检索到Datagridview [Windows应用程序] [英] How to retrieve image from SQL Server 2008 R2 to Datagridview in C# [Windows Application]
问题描述
朋友们,我已经将图像插入到Sql Server中,我需要在C#[Windows应用程序]中将图像从SQL Server 2008 R2检索到Datagridview列。是否有可能在数据网格视图中显示图像是任何相应的大小网格视图的一列。
将图像插入SQL Server的编码是:
< pre lang =c#> 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 = 插入tbl_image值(' + imagedata + ');
MessageBox.Show(query);
SqlCommand cmd = new SqlCommand(query,sqlconn);
int rows = cmd.ExecuteNonQuery();
如果(行> 0 )
{
MessageBox.Show( 图片保存。) ;
txt_upload.Text = ;
pictureBox1.Image = null ;
}
else
{
MessageBox.Show( 无法保存图像。);
sqlconn.Close();
}
}
public byte [] ReadImageFile( string imageLocation)
{
byte [] imageData = 空跨度>;
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;
}
请尝试解决此问题。谢谢哦亲爱的,亲爱的,哦亲爱的。
再次开始:你的数据库里到处都是垃圾。
byte [] imagedata = ReadImageFile(txt_upload.Text);
...
string query = insert into tbl_image values(' + imagedata + ');
你不能只是将一个字节数组连接成一个字符串:你得到的查询是:
< span class =code-keyword> insert into tbl_image values (' System.Byte []')
哪个不是图像,永远不会是图像无论你尝试什么,它都不能用作图像。
使用参数化查询:这种方式传输数据,你将减少SQL注入攻击的可能性。
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 = 插入tbl_image值(@ID)跨度>;
MessageBox.Show(query);
SqlCommand cmd = new SqlCommand(query,sqlconn);
cmd.Parameters.AddWithValue( @ ID,imagedata);
int rows = cmd.ExecuteNonQuery();
当您对其进行排序时,您可以开始查看读回数据。
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.
My Coding to Insert the images to SQL Server is :
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
Oh dear, oh dear, oh dear.
Start again: your database is currently full of rubbish.
byte[] imagedata = ReadImageFile(txt_upload.Text); ... string query = "insert into tbl_image values('"+imagedata+"')";You can''t just concatenate a byte array into a string: what you get as a query is:
insert into tbl_image values('System.Byte[]')Which is not an image, never will be an image and will not work as an image no matter what you try.
Use a parametrized query instead: that way teh data is transferred, and you will have less chance of an SQL Injection attack.
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(@ID)"; MessageBox.Show(query); SqlCommand cmd = new SqlCommand(query, sqlconn); cmd.Parameters.AddWithValue("@ID", imagedata); int rows = cmd.ExecuteNonQuery();When you have sorted that out, you can start looking at reading the data back.
这篇关于如何在C#中将图像从SQL Server 2008 R2检索到Datagridview [Windows应用程序]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!