0

我想从多个表中检索数据并想生成一个水晶报表。因此,每次我需要生成报告时,我都创建了一个新表并在其中插入值。所以我使用以下代码从这些表中检索数据。

代码:

 Private Sub gen_Report()

    Dim dr, dr1, dr2 As OleDb.OleDbDataReader
    Dim cmd, cmdDel, comm_inv1, comm_invuser As OleDb.OleDbCommand

    If cnnOLEDB.State = ConnectionState.Closed Then
        cnnOLEDB.Open()
    End If
    Dim strDelInsRp As String = ("DELETE  FROM Inst_Report")
    cmdDel = New OleDb.OleDbCommand(strDelInsRp, cnnOLEDB)
    cmdDel.Parameters.AddWithValue("@chlno", cmbChal_no.Text)
    cmdDel.ExecuteNonQuery()

    Dim strSelIns As String = ("SELECT * FROM Installation_det where Chalan_No=@chlno")
    cmd = New OleDb.OleDbCommand(strSelIns, cnnOLEDB)
    cmd.Parameters.AddWithValue("@chlno", cmbChal_no.Text)


    dr = cmd.ExecuteReader
    Try

        Do While dr.Read = True

            mach_srno = dr("Machine_SrNo")
            tft_srno = dr("TFT_SrNo")
            chl_no = dr("Chalan_No")
            usernm = dr("User_Name")
            ins_dt = dr("Date_Of_Installation")
            war_perd = dr("Warranty_Period")
            war_till = dr("Warranty_Valid_Till")

            Dim strSelInv1 As String = ("SELECT * FROM INVOICE_ONE where LAY_NO='VDC' AND CHL_NO=@chn_no ")
            comm_inv1 = New OleDb.OleDbCommand(strSelInv1, cnnOLEDB)
            comm_inv1.Parameters.AddWithValue("@chn_no", chl_no)

            dr1 = comm_inv1.ExecuteReader
            If dr1.Read = True Then

                doc_no = dr1("DOCU_NO")
                code_no = dr1("CODE_NO")
                memb_nm = dr1("MEMB_NM")

                Dim strSelInvUser As String = ("SELECT * FROM INVOICE_USER where CODE_NO=@code AND LAY_NO='VDC' AND DOCU_NO=@docno")
                comm_invuser = New OleDb.OleDbCommand(strSelInvUser, cnnOLEDB)
                comm_invuser.Parameters.AddWithValue("@code", code_no)
                comm_invuser.Parameters.AddWithValue("@docno", doc_no)

                dr2 = comm_invuser.ExecuteReader

                If dr2.Read = True Then
                    User_add = dr2("ILEN2") & dr2("ILEN3") & dr2("ILEN4") & dr2("ILEN5")
                End If
                dr2.Close()
            End If
            dr1.Close()
            Dim strInsRep As String = "INSERT INTO Inst_Report(Mach_srNo,TFT_srNo,Mem_nm,UserNm,Dt_Inst,War_Per,war_till,User_Address) VALUES (@mach_srno,@tft_no,@mem_nm,@uname,@inst_dt,@war_per,@war_till,@address)"
            Dim comm_InsRep As OleDb.OleDbCommand = New OleDb.OleDbCommand(strInsRep, cnnOLEDB)

            comm_InsRep.Parameters.AddWithValue("@mach_srno", mach_srno)
            comm_InsRep.Parameters.AddWithValue("@tft_no", tft_srno)
            comm_InsRep.Parameters.AddWithValue("@mem_nm", memb_nm)
            comm_InsRep.Parameters.AddWithValue("@uname", usernm)
            comm_InsRep.Parameters.AddWithValue("@inst_dt", ins_dt)
            comm_InsRep.Parameters.AddWithValue("@war_per", war_perd)
            comm_InsRep.Parameters.AddWithValue("@war_till", war_till)
            comm_InsRep.Parameters.AddWithValue("@address", User_add)


            comm_InsRep.ExecuteNonQuery()


        Loop
        dr.Close()

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

但问题是即使我使用'do While Loop',数据读取器也只会读取第一条记录。

我有另一个问题:

随着字段数量的增加,我想以横向生成报告。那么如何改变报告的方向。

我正在使用 Visual Studio 2005 和 MS-Access 2007。编程语言是 VB.NET。

4

1 回答 1

0

关于您的数据,我相信从 OleDbDataReader 获取数据的标准方法是使用它来填充数据表,试试这个:

Private Function GetDataTableUsingDataReader(ByVal command As OleDbCommand) As DataTable
    Dim dataTable As DataTable = New DataTable

    Using reader As OleDbDataReader = command.ExecuteReader
        dataTable.Load(reader, LoadOption.OverwriteChanges)
    End Using

    Return dataTable
End Function

要将您的水晶报表变为横向,请在设计器中右键单击报表,转到设计 => 页面设置。从这里您可以更改方向。

于 2013-06-21T08:43:19.323 回答