我想从多个表中检索数据,并希望生成水晶报告。因此,我每次需要生成报告时都创建了一个新表并在其中插入值。所以我使用以下代码从这些表中检索数据。
代码:
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',datareader也只读取第一条记录。
我有另一个问题:
由于字段数量更多,我想在横向方向生成报告。 那么如何改变报告的方向。
我使用的是Visual Studio 2005和MS-Access 2007.编程语言是VB.NET。
答案 0 :(得分: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
要将水晶报告设置为横向,请右键单击设计器中的报告,goto design =>页面设置。从这里你可以改变方向。