我正在C#中创建一个来自学生登录时生成PDF的文件。我完成了模板,我的问题是如何将数据库中的数据放到我的PDF中。
PDF中需要提供的数据是学生的姓名,学生的课程,学分,以及他/她所注册的所有科目。
所有数据都存储在学生和登记表中。 这是表格:
mysql> describe enroll;
+-----------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+---------+------+-----+---------+----------------+
| Eid | int(11) | NO | PRI | NULL | auto_increment |
| Students_studid | int(11) | NO | MUL | NULL | |
| Subjects_Subjid | int(11) | NO | MUL | NULL | |
+-----------------+---------+------+-----+---------+----------------+
和
mysql> describe students;
+------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+----------------+
| studid | int(11) | NO | PRI | NULL | auto_increment |
| StudName | varchar(45) | YES | | NULL | |
| StudCourse | varchar(45) | YES | | NULL | |
| StudYr | int(11) | YES | | NULL | |
| StudGender | varchar(45) | YES | | NULL | |
+------------+-------------+------+-----+---------+----------------+
这是我的生成PDF表单:
class Form_PDF : Form {
public Form_PDF(){
Text = "PDF";
CenterToScreen();
System.IO.FileStream fs = new FileStream(""+Form_Login.getUser+".pdf", FileMode.Create);
Document document = new Document(PageSize.A4, 25, 25, 30, 30);
PdfWriter writer = PdfWriter.GetInstance(document, fs);
document.AddAuthor("The Author");
document.AddCreator("using iTextSharp");
document.AddKeywords("C# PDF");
document.AddSubject("C# PDF");
document.AddTitle("PDF creation using iTextSharp");
document.Open();
PdfContentByte cb = writer.DirectContent;
PdfContentByte xy = writer.DirectContent;
//BaseFont f_cb = BaseFont.CreateFont("c:\\windows\\fonts\\calibri.ttf", BaseFont.CP1252, BaseFont.EMBEDDED);
BaseFont f_cn = BaseFont.CreateFont("c:\\windows\\fonts\\calibri.ttf", BaseFont.CP1252, BaseFont.EMBEDDED);
//XY
xy.BeginText();
xy.SetFontAndSize(f_cn, 9);
xy.SetTextMatrix(110, 661); // Left, Top
xy.ShowText("Student Name: " +Form_Login.getUser);
xy.SetTextMatrix(110, 637); // Left, Top
xy.ShowText("Semester: ");
xy.SetTextMatrix(230, 661); // Left, Top
xy.ShowText("Course: ");
sqlConnection1.Close();
xy.SetTextMatrix(230, 637); // Left, Top
xy.ShowText("Section: ");
xy.SetTextMatrix(230, 613); // Left, Top
xy.ShowText("Division: ");
xy.SetTextMatrix(375, 661); // Left, Top
xy.ShowText("School Year: ");
xy.SetTextMatrix(375, 637); // Left, Top
xy.ShowText("Credit Units: ");
cb.SetLineWidth(0f);
cb.MoveTo(100, 601);
cb.LineTo(510, 601);
cb.Stroke();
xy.SetTextMatrix(120, 577); // Left, Top
xy.ShowText("Subject Code");
xy.SetTextMatrix(195, 577); // Left, Top
xy.ShowText("Subject Description");
xy.SetTextMatrix(310, 577); // Left, Top
xy.ShowText("Subject Schedule");
xy.SetTextMatrix(405, 577); // Left, Top
xy.ShowText("Room #");
xy.SetTextMatrix(470, 577); // Left, Top
xy.ShowText("Units");
cb.SetLineWidth(0f);
cb.MoveTo(100, 560);
cb.LineTo(510, 560);
cb.Stroke();
xy.SetTextMatrix(120, 313); // Left, Top
xy.ShowText("NOTE: Assessment on this Registration Card is TEMPORARY only. Final assessment will be determined by");
xy.SetTextMatrix(145, 301); // Left, Top
xy.ShowText("the Finance office.");
cb.SetLineWidth(0f);
cb.MoveTo(400, 253);
cb.LineTo(510, 253);
cb.Stroke();
xy.SetTextMatrix(425, 241); // Left, Top
xy.ShowText("Registrar Officer");
int row = 1;
for (int y = 0; y != 70; y++)
{
xy.SetTextMatrix(10, row);
xy.ShowText("Y: " + row.ToString());
row += 12; // The spacing between the rows is set to 12 "points"
}
int col = 35;
for (int x = 0; x != 22; x++)
{
xy.SetTextMatrix(col, 829);
xy.ShowText("X: " + col.ToString());
col += 25; // The spacing between the columns is set to 25 "points"
}
xy.EndText();
cb.BeginText();
cb.SetFontAndSize(f_cn, 16);
//START DIRI
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance("C:\\Users\\db\\Desktop\\addu-logo.jpg");
img.SetAbsolutePosition(185, 697);
img.ScalePercent(10f);
cb.AddImage(img);
cb.SetTextMatrix(240, 733); // Left, Top
cb.ShowText("Ateneo de Davao University");
cb.SetTextMatrix(280, 718);
cb.ShowText("Registrar's Office");
cb.SetTextMatrix(266, 703);
cb.ShowText("Student Registration");
cb.EndText();
//document.Add(new Paragraph("Ateneo de Davao University"));
document.Close();
writer.Close();
fs.Close();
}
}
答案 0 :(得分:0)
这只是一个例子:
using System;
using MySql.Data.MySqlClient;
public class Example
{
static void Main()
{
string cs = @"server=localhost;userid=user12;
password=34klq*;database=mydb";
MySqlConnection conn = null;
MySqlDataReader rdr = null;
try
{
conn = new MySqlConnection(cs);
conn.Open();
string stm = "SELECT * FROM Authors";
MySqlCommand cmd = new MySqlCommand(stm, conn);
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Console.WriteLine(rdr.GetInt32(0) + ": "
+ rdr.GetString(1));
}
} catch (MySqlException ex)
{
Console.WriteLine("Error: {0}", ex.ToString());
} finally
{
if (rdr != null)
{
rdr.Close();
}
if (conn != null)
{
conn.Close();
}
}
}
}
请参阅合作信息
http://zetcode.com/db/mysqlcsharptutorial/
来自mysql的数据将在此
中出现while (rdr.Read())
{
Console.WriteLine(rdr.GetInt32(0) + ": "
+ rdr.GetString(1));
}
这里循环将让你逐个记录,
更改代码
Console.WriteLine(rdr.GetInt32(0)+":" + rdr.GetString(1));
你想要的例子