简体   繁体   English

在VB.NET 2010中打印

[英]Print in VB.NET 2010

I want to print on a Passbook (paper book used to record bank transactions) using a passbook printer (Epson PLQ 20) Using VB.NET 2010. 我想使用存折打印机(Epson PLQ 20)使用VB.NET 2010在Passbook(用于记录银行交易的纸质书)上打印。

My current mysql table structure is, 我目前的mysql表结构是,

1. tblLoanRegistry(LoanID pk, EMPNumber, Date, Amount, NoOfInstallments, Teller)
2. tblLoanAccount(ID pk, LoanID fk, Date, Payment, Interest, Total, Auto bool, Installment int, teller)

My problems are: 我的问题是:

  1. How to detect the last row printed? 如何检测最后一行打印?
  2. How to print the first row that not printed, on the correct position of the book (Correct line). 如何在书的正确位置(正确的行)上打印未打印的第一行。

I have decided to add a field "Printed" (Boolean) in each table above mentioned. 我决定在上面提到的每个表中添加一个字段“Printed”(布尔)。 To get the printed or not. 要打印还是不打印。 I can print text, numbers etc using the same printer in vb.net (Eg: Account holders details on the front page). 我可以在vb.net中使用相同的打印机打印文本,数字等(例如:首页上的帐户持有人详细信息)。 But I'm facing above mentioned problems when printing transactions. 但是我在打印交易时面临上述问题。 Your help/ opinions highly appreciated. 您的帮助/意见非常感谢。

More Information: Actually I developed a web based account handling system using php and mysql for a non profit organisation as the my project of the degree. 更多信息:实际上我开发了一个基于Web的帐户处理系统,使用php和mysql作为我的学位项目。 Now they want to print transactions on a passbook as I described before. 现在他们想要像我之前描述的那样在存折上打印交易。

Therefore I am creating an application using VB.NET (I am totally new to VB.NET. But have experience in vb6) while I am learning it. 因此,我正在使用VB.NET创建一个应用程序(我对VB.NET很新。但是在vb6中有经验),而我正在学习它。 I have managed to simple printing but this is something different. 我已经设法简单打印,但这是不同的。

I have no good idea to solve above mentioned two problems. 我没有好主意解决上面提到的两个问题。

Update: I did it in different (may be a bad) way. 更新:我以不同的方式(可能是一个坏的方式)做到了。 On click event of the print button. 在打印按钮的单击事件上。

    Dim sqlLoan As String

    conn = New MySqlConnection(cnString)

    sqlLoan = "SELECT tblLoanAccount.Date,if(Installment = 0, 'Interest', concat('Installment : ', Installment)) as Description, tblLoanAccount.Payment, tblLoanAccount.Interest, " &
        " tblLoanAccount.Total, tblLoanAccount.Auto, tblLoanAccount.Installment FROM tblLoanAccount join tblloanRegistry on  tblloanRegistry.LoanID = tblLoanAccount.LoanID " &
        " where(tblloanRegistry.EMPNumber = " & cmbEMPN.Text & " And tblLoanAccount.LoanID = tblLoanRegistry.LoanID) AND tblLoanAccount.Total <> 0 ORDER BY tblLoanAccount.ID"

    Using conn As New MySqlConnection(cnString)
        Using cmd As New MySqlCommand(sqlLoan, conn)
            conn.Open()
            Using myReader As MySqlDataReader = cmd.ExecuteReader()
                Using writer As StreamWriter = New StreamWriter("c:\file.txt")
                    While myReader.Read()
                        writer.WriteLine("{0}, {1}, {2}, {3}, {4}", myReader.Item(0), myReader.Item(1), myReader.Item(2), myReader.Item(3), myReader.Item(4))
                    End While
                End Using
                Call Printing()
            End Using
        End Using
    End Using

    ' Print the file. 
Public Sub Printing()
    Try
        streamToPrint = New StreamReader(("c:\file.txt"))
        Try
            printFont = New Font("Arial", 10)
            Dim pd As New PrintDocument()
            AddHandler pd.PrintPage, AddressOf pd_PrintPage
            ' Print the document.
            pd.Print()
        Finally
            streamToPrint.Close()
        End Try
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub 'Printing

And other codes are as msdn PrintDocument Class . 其他代码如msdn PrintDocument Class

There are a lot of unknowns in this question. 这个问题有很多未知数。 For example, when you mark an item as printed, do you want to mark it universally (for all applications, all transactions and all time) or only within a limited context (a particular application, transaction or time frame)? 例如,当您将项目标记为已打印时,是否要将其标记为普遍(对于所有应用程序,所有事务和所有时间)或仅在有限的上下文(特定应用程序,事务或时间范围)内?

Is a transaction started and completed within a single .NET thread, or is this application multi-threaded, or does the transaction span across multiple independent executions? 事务是在单个.NET线程中启动和完成的,还是此应用程序是多线程的,还是事务跨越多个独立执行? Do you need some record of an item having been printed beyond the physical piece of paper? 您是否需要在物理纸张之外打印物品的某些记录?

Assuming you want a record of an item having been printed only within a particular transaction, you may want to create a third table, called something like tblPrintTransaction, with a Primary Key or other identifier column and any additional columns you desire (transaction start date, end date, user ID, contextual information, etc.). 假设您想要仅在特定事务中打印的项目记录,您可能需要创建第三个表,称为tblPrintTransaction,具有主键或其他标识符列以及您想要的任何其他列(事务开始日期,结束日期,用户ID,上下文信息等)。 When you start your application, create a new row in this table and get the row ID. 启动应用程序时,在此表中创建一个新行并获取行ID。

Now, create a fourth table, called something like tblPrintTransactionArtifact, with at least two columns. 现在,创建第四个表,称为tblPrintTransactionArtifact,至少有两列。 One column will be a foreign key identifying the transaction (from the tblPrintTransaction table) and one or more columns will be used to identify the item that has been printed. 一列将是标识事务的外键(来自tblPrintTransaction表),一列或多列将用于标识已打印的项目。 For example, your table could contain two columns to identify the printed item: one column specifying either "Registry" or "Account" and another specifying the item's ID. 例如,您的表可以包含两列来标识打印的项目:一列指定“注册表”或“帐户”,另一列指定项目的ID。

Of course, all of this information could be created and maintained within the application itself (using variables, etc), but storing them in a table means they will persist beyond the execution of the application, giving you a permanent record. 当然,所有这些信息都可以在应用程序本身内创建和维护(使用变量等),但将它们存储在表中意味着它们将持续超出应用程序的执行,从而为您提供永久记录。 I would recommend that you keep track of the current "line" on the printed page within the application as I see little use for this within your database. 我建议您跟踪应用程序中打印页面上的当前“行”,因为我在您的数据库中看不到这一点。

Dim sqlLoan As String Dim sqlLoan As String

conn = New MySqlConnection(cnString)

sqlLoan = "SELECT tblLoanAccount.Date,if(Installment = 0, 'Interest', concat('Installment : ', Installment)) as Description, tblLoanAccount.Payment, tblLoanAccount.Interest, " &
    " tblLoanAccount.Total, tblLoanAccount.Auto, tblLoanAccount.Installment FROM tblLoanAccount join tblloanRegistry on  tblloanRegistry.LoanID = tblLoanAccount.LoanID " &
    " where(tblloanRegistry.EMPNumber = " & cmbEMPN.Text & " And tblLoanAccount.LoanID = tblLoanRegistry.LoanID) AND tblLoanAccount.Total <> 0 ORDER BY tblLoanAccount.ID"

Using conn As New MySqlConnection(cnString)
    Using cmd As New MySqlCommand(sqlLoan, conn)
        conn.Open()
        Using myReader As MySqlDataReader = cmd.ExecuteReader()
            Using writer As StreamWriter = New StreamWriter("c:\file.txt")
                While myReader.Read()
                    writer.WriteLine("{0}, {1}, {2}, {3}, {4}", myReader.Item(0), myReader.Item(1), myReader.Item(2), myReader.Item(3), myReader.Item(4))
                End While
            End Using
            Call Printing()
        End Using
    End Using
End Using

    ' Print the file. 
Public Sub Printing()
    Try
        streamToPrint = New StreamReader("c:\file.txt")
        Try
            printFont = New Font("Arial", 10)
            Dim pd As New PrintDocument()
            AddHandler pd.PrintPage, AddressOf pd_PrintPage
            ' Print the document.
            pd.Print()
        Finally
            streamToPrint.Close()
        End Try
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub 'Printing

And other codes are as msdn PrintDocument Class. 其他代码如msdn PrintDocument Class。

您可以尝试使用我在http://www.jcl.vdtec.net上使用MySql和VB.Net的易于使用的轻量级报表编写器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM