使用c#从sql server检索大于1mb的图像 [英] Retrieve image of greater than 1mb from sql server using c#

查看:62
本文介绍了使用c#从sql server检索大于1mb的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用c#从windpws应用程序中的sql server检索大小超过1 mb的图像。

当我尝试插入大小超过1 mb的图像时,会插入,但是当我尝试检索图像时,发生运行时错误,即参数无效。

我试图检索图像的代码。





如何检索更大尺寸的图像,即大于1 mb的图像。



先谢谢。 .. !!!



我尝试过:



< pre lang =c#> name = cbCust_Name.SelectedValue.ToString();
// btnDisplay_Click(sender,e);
SqlConnection cnn = new SqlConnection(conString);
MemoryStream stream = new MemoryStream();
cnn.Open();
SqlCommand command = new SqlCommand( 选择来自Photo的Cust_Image,Cust_Name =' + name + ',cnn);
byte [] image =( byte [])command.ExecuteScalar();
stream.Write(image, 0 ,image.Length);
// cnn.Close();
位图位图= 位图(流);
pictureBox1.Image = bitmap;

解决方案

问题是新的Bitmap(ms)将从中读取数据流的当前位置。如果流当前位于数据的末尾,则它将无法读取任何内容,因此会出现问题。请尝试使用以下代码:

  string  name = cbCust_Name.SelectedValue.ToString(); 

SqlConnection cnn = new SqlConnection(conString);
cnn.Open();
SqlCommand command = new SqlCommand( 选择来自Photo的Cust_Image,Cust_Name =' + name + ',cnn);
byte [] image =( byte [])command.ExecuteScalar();
cnn.Close();

MemoryStream ms = new MemoryStream(image);
ms.Seek( 0 ,SeekOrigin.Begin);
位图位图= 位图(ms);
pictureBox1.Image = bitmap;



建议:在使用内联SQL查询时,请尝试使用参数化查询。它有助于避免SQL注入。其次,正确关闭连接和内存流对象。


I want to retrieve images of greater than 1 mb in size from sql server in windpws application using c#.
When I try to insert the image of greater than 1 mb in size, it is inserted, but when I try to retrieve the image the runtime error is occurring i.e. "Parameter is not valid".
The code I tried to retrieve the image.


How can I retrieve the image of bigger size i.e. greater than 1 mb in size.

Thanks in Advance ...!!!

What I have tried:

name = cbCust_Name.SelectedValue.ToString();  
//btnDisplay_Click(sender, e);  
SqlConnection cnn = new SqlConnection(conString);  
MemoryStream stream = new MemoryStream();  
cnn.Open();  
SqlCommand command = new SqlCommand("Select Cust_Image from Photo where Cust_Name= '" + name + "'", cnn);  
byte[] image = (byte[])command.ExecuteScalar();  
stream.Write(image, 0, image.Length);  
//cnn.Close();  
Bitmap bitmap = new Bitmap(stream);  
pictureBox1.Image = bitmap;

解决方案

The problem is that new Bitmap(ms) is going to read the data from the stream's current position. And if the stream is currently positioned at the end of the data, it's not going to be able to read anything, hence the problem occurs. Try with below code:

string name = cbCust_Name.SelectedValue.ToString();  

SqlConnection cnn = new SqlConnection(conString);
cnn.Open();  
SqlCommand command = new SqlCommand("Select Cust_Image from Photo where Cust_Name= '" + name + "'", cnn);  
byte[] image = (byte[])command.ExecuteScalar();
cnn.Close();  

MemoryStream ms = new MemoryStream(image);
ms.Seek(0, SeekOrigin.Begin);
Bitmap bitmap = new Bitmap(ms);
pictureBox1.Image = bitmap;


Suggestions: Please try to use Parameterised query when you are using inline SQL query. It helps to avoid SQL injection. Secondly close the connection and memory stream object properly.


这篇关于使用c#从sql server检索大于1mb的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆