在 VB.NET 2010 中打印
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14042362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Print in VB.NET 2010
提问by nirosha rathnayaka
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.
我想使用 VB.NET 2010 使用存折打印机 (Epson PLQ 20) 在存折(用于记录银行交易的纸本)上打印。
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:
我的问题是:
- How to detect the last row printed?
- How to print the first row that not printed, on the correct position of the book (Correct line).
- 如何检测打印的最后一行?
- 如何在书的正确位置(Correct line)打印未打印的第一行。
I have decided to add a field "Printed" (Boolean) in each table above mentioned. 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). But I'm facing above mentioned problems when printing transactions. Your help/ opinions highly appreciated.
我决定在上面提到的每个表中添加一个字段“Printed”(布尔值)。打印与否。我可以在 vb.net 中使用同一台打印机打印文本、数字等(例如:首页上的帐户持有人详细信息)。但是我在打印交易时面临上述问题。非常感谢您的帮助/意见。
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. Now they want to print transactions on a passbook as I described before.
更多信息:实际上,我为一个非营利组织开发了一个使用 php 和 mysql 的基于 Web 的帐户处理系统作为我的学位项目。现在他们想像我之前描述的那样在存折上打印交易。
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. I have managed to simple printing but this is something different.
因此,我正在使用 VB.NET 创建一个应用程序(我对 VB.NET 完全陌生。但在学习 vb6 时有经验)。我设法进行了简单的打印,但这是不同的。
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。
采纳答案by nirosha rathnayaka
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。
回答by JDB still remembers Monica
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? Do you need some record of an item having been printed beyond the physical piece of paper?
事务是在单个 .NET 线程中启动和完成的,还是该应用程序是多线程的,还是事务跨越多个独立的执行?您是否需要一些超出物理纸张打印出来的项目的记录?
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.). When you start your application, create a new row in this table and get the row ID.
假设您希望记录仅在特定事务中打印的项目,您可能希望创建第三个表,称为 tblPrintTransaction,带有主键或其他标识符列以及您想要的任何其他列(事务开始日期、结束日期、用户 ID、上下文信息等)。当您启动应用程序时,在此表中创建一个新行并获取行 ID。
Now, create a fourth table, called something like tblPrintTransactionArtifact, with at least two columns. 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. 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.
现在,创建第四个表,称为 tblPrintTransactionArtifact 之类的表,至少有两列。一列将是标识事务的外键(来自 tblPrintTransaction 表),一列或多列将用于标识已打印的项目。例如,您的表可以包含两列来标识打印的项目:一列指定“注册表”或“帐户”,另一列指定项目的 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.
当然,所有这些信息都可以在应用程序本身内创建和维护(使用变量等),但将它们存储在表中意味着它们将在应用程序执行后持续存在,为您提供永久记录。我建议您跟踪应用程序中打印页面上的当前“行”,因为我认为在您的数据库中很少使用它。
回答by JCL
You can try to use the easy to use, lightweight report writer I use with MySql & VB.Net at http://www.jcl.vdtec.net
您可以尝试使用我在http://www.jcl.vdtec.net 上与 MySql 和 VB.Net 一起使用的易于使用的轻量级报告编写器

