如何从访问数据库中插入水晶报表中的图像。 [英] How to insert image in crystal report from access database.

查看:79
本文介绍了如何从访问数据库中插入水晶报表中的图像。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在访问数据库中创建了表Image,它有3个字段ImageId,ImageName和Image_Path。我在下面添加了以下代码:

I have created table "Image" in access database that have 3 fields "ImageId","ImageName" and "Image_Path". I added below code to form:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Con As OdbcConnection = New OdbcConnection
        Dim Sql As String = "Select * from Image"
        Con.ConnectionString = connstring
        Dim da As OdbcDataAdapter = New OdbcDataAdapter(Sql, Con)

        da.Fill(ds, "Image")
        AddImageColumn(ds.Tables("Image"), "Image_stream")
        ' Loop to load the image for each row
        For index As Integer = 0 To ds.Tables("Image").Rows.Count - 1
            If Not String.IsNullOrEmpty(ds.Tables("Image").Rows(index).Item("Image_Path").ToString) Then
                'LoadImage(ds.Tables("Image").Rows(index), "Image", _
                'ds.Tables("Image").Rows(index).Item("Image_Path").ToString)
                LoadImage(ds.Tables("Image").Rows(index), "Image_stream", ds.Tables("Image").Rows(index).Item("Image_Path").ToString)

            Else
                
            End If
        Next
        '* End code to add
        rptDoc.ReportSource = ds.Tables("Image")
        ' Display report in a report viewer control
    End Sub
    Private Sub AddImageColumn(ByVal objDataTable As DataTable, ByVal strFieldName As String)
        Try
            Dim objDataColumn As DataColumn = New DataColumn(strFieldName, Type.GetType("System.Byte[]"))
            objDataTable.Columns.Add(objDataColumn)
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try
    End Sub

    Private Sub LoadImage(ByVal objDataRow As DataRow, ByVal strImageField As String, ByVal FilePath As String)
        Try
            Dim fs As System.IO.FileStream = New System.IO.FileStream(FilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read)
            Dim Image() As Byte = New Byte(fs.Length) {}
            fs.Read(Image, 0, CType(fs.Length, Integer))
            fs.Close()
            objDataRow(strImageField) = Image
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try
    End Sub





但我收到了filenotfound错误:



But I get filenotfound error on line:

Dim fs As System.IO.FileStream = New System.IO.FileStream(FilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read)



虽然该路径包含image.Can any1可以帮助我吗?


Although that path contains image.Can any1 help me please?

推荐答案

如果您在数据库中直接存储图像不是图像的路径;

然后



这很容易,



只传递具有图像列的数据源(例如,格式是二进制)和水晶报表中数据源的拖放字段,就像你为一个简单的文本字段做的那样



快乐编码!

:)
if you have stored image directly in database not a path of image;
then

It is quite easy,

just pass data-source having image column (e.g. format is binary) and drag-drop field from datasource in crystal report as you do for a simple text field

Happy Coding!
:)


这篇关于如何从访问数据库中插入水晶报表中的图像。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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