Crystal报表显示SQL的图像 [英] Crystal reports display image from SQL

查看:96
本文介绍了Crystal报表显示SQL的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个ac#winform应用程序,它将图像作为图像存储在sql数据库中。

我可以存储图像很好但是当打印报告时图像不会出现。



任何人都可以提前帮助。

用于存储图像和打印报告的代码如下:



我尝试过:



//在SQL中插入图片:

 con.Open(); 
MemoryStream ms = new MemoryStream();
pictureBox2.Image.Save(ms,ImageFormat.Jpeg);
byte [] photo = new byte [ms.Length];
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = 插入dbo.temp(maquete)VALUES(' + Photo + ');
cmd.ExecuteNonQuery();
con.Close();
报告报告=新报告();
report.ShowDialog();





< br $>
//打印报告



 con.Open(); 
SqlCommand cmd = con。 CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = 选择*来自temp;
cmd.ExecuteNonQuery();
bdDataSet ds = new bdDataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds.temp);
hytherm myReport = new hytherm();
myReport.SetDataSource(ds);
crystalReportViewer1.ReportSource = myReport;
con.Close( );

解决方案

从不使用字符串连接来构建SQL查询。 总是使用参数化查询。



目前,使用字符串连接,您的最终查询将是:

 插入 进入 dbo.temp(maquete) VALUES '  System.Byte []'



显然,文字字符串 System.Byte [] 不是有效的字节数组。



(字节数组也是空的 - 你声明它,但你永远不会填充它。)



为了将一个字节数组传递给你的查询,你必须使用一个参数:

 使用 var  connection =  new  SqlConnection(   ...))
使用 var command = new SqlCommand( INSERT INTO dbo.temp(maquete)VALUES(@photo),connection))
{
using var ms = new MemoryStream())
{
pictureBox2.Image.Save (ms,ImageFormat.Jpeg);
byte [] photo = ms.ToArray();
command.Parameters.AddWithValue( @ photo,photo);
}

connection.Open();
command.ExecuteNonQuery();
}





NB:想到的所有图片到目前为止,你存储在你的表中已经腐败了。你需要删除并重新添加它们。




你想知道关于SQL注入的一切(但不敢问)特洛伊亨特 [ ^ ]

如何在没有技术术语的情况下解释SQL注入? |信息安全堆栈交换 [ ^ ]

查询参数化备忘单| OWASP [ ^ ]


在Crystal Reports中存储和检索图像从SQL数据库和使用ASP.Net的文件夹 [ ^ ]





 tp: //   www.aspsnippets.com/Articles/Display-image-from-database-在晶-报告功能于ASPNET-使用-C-和-VBNet.aspx  


Hi,

I have a c# winform app that stores images in an sql database as "image".
I can store the image just fine but when a print the report the image does not appear.

Can anyone help, thanks in advance.
The code i use to store the image and print the report is below:

What I have tried:

//insert image in SQL:

con.Open();
MemoryStream ms = new MemoryStream();
pictureBox2.Image.Save(ms, ImageFormat.Jpeg);
byte[] photo = new byte[ms.Length];
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into dbo.temp (maquete) VALUES ('" + Photo + "');
cmd.ExecuteNonQuery();
con.Close();
report report = new report();
report.ShowDialog();




//print report

con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from temp";
cmd.ExecuteNonQuery();
bdDataSet ds = new bdDataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds.temp);
hytherm myReport = new hytherm();
myReport.SetDataSource(ds);
crystalReportViewer1.ReportSource = myReport;
con.Close();

解决方案

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

At the moment, using string concatenation, your final query will be:

insert into dbo.temp (maquete) VALUES ('System.Byte[]')


Clearly, the literal string System.Byte[] is not a valid byte array.

(The byte array is also empty - you declare it, but you never populate it.)

In order to pass a byte array to your query, you must use a parameter:

using (var connection = new SqlConnection("..."))
using (var command = new SqlCommand("INSERT INTO dbo.temp (maquete) VALUES (@photo)", connection))
{
    using (var ms = new MemoryStream())
    {
        pictureBox2.Image.Save(ms, ImageFormat.Jpeg);
        byte[] photo = ms.ToArray();
        command.Parameters.AddWithValue("@photo", photo);
    }
    
    connection.Open();
    command.ExecuteNonQuery();
}



NB: All of the images that you think you've stored in your table so far are corrupt. You will need to remove and re-add them.


Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]


Store and Retrieve Image in Crystal Reports From SQL Database and Folder Using ASP.Net[^]


tp://www.aspsnippets.com/Articles/Display-image-from-database-in-Crystal-Report-in-ASPNet-using-C-and-VBNet.aspx


这篇关于Crystal报表显示SQL的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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