如何将数据从数据库显示到Crystal报表? [英] How Do I Display Image From Database To Crystal Report?
本文介绍了如何将数据从数据库显示到Crystal报表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hello先生,
为了存储图像,我使用了字节格式和图像字段的数据类型是varchar(MAX)。但是在水晶报表图像中没有显示。我正在开发这是在WINDOWS FORM。
请帮帮我。
Hello Sir,
For storing the image I used byte format & datatype of image field is varchar(MAX).But in crystal report image is not displayed.And I am developing this is in WINDOWS FORM.
Please help me.
推荐答案
您好,
有看一下这个 [ ^ ]关于ASP片段的文章。
问候,
Hello,
Have a look at this[^] article on ASP Snippets.
Regards,
public byte[] convertImageToByteArray(System.Drawing.Image image)
{
MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
// or whatever output format you like
return ms.ToArray();
}
//Save Data
private void button1_Click(object sender, EventArgs e)
{
byte[] imgbnry = convertImageToByteArray(pictureBox1.Image);
string str = "insert into tbl_img (id,photo)values('" + textBox1.Text + "',@img)";
string ConStr = @"Server=COMP7;Database=ImageTest;User Id=sa;Password=cos123";
SqlConnection con = new SqlConnection(ConStr);
con.Open();
SqlCommand cmd = new SqlCommand(str, con);
cmd.Parameters.AddWithValue("@img", imgbnry);
int n = cmd.ExecuteNonQuery();
if (n > 0)
{
MessageBox.Show("Successfully inserted");
}
else
{
MessageBox.Show("Not inserted");
}
}
//retrive Data
Collapse | Copy Code
private void button2_Click(object sender, EventArgs e)
{
string str = "select photo from tbl_img where id='" + textBox2.Text + "'";
string ConStr = @"Server=COMP7;Database=ImageTest;User Id=sa;Password=cos123";
SqlConnection con = new SqlConnection(ConStr);
con.Open();
SqlDataAdapter da = new SqlDataAdapter(str, con);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
byte[] imgdata = new byte[0];
imgdata = (byte[])dt.Rows[0][0];
MemoryStream ms = new MemoryStream(imgdata);
pictureBox2.Image = Image.FromStream(ms);
}
else
{
MessageBox.Show("No images in a table");
}
}
使用图像数据类型在sql server上存储图像。
将此表附加到水晶报告。
use image data type to store image at sql server.
attach this table to crystal report.
这篇关于如何将数据从数据库显示到Crystal报表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文