[英]How do I add sql query data to a table in itextsharp pdf creator?
我的數據庫中有多個表,需要從中檢索數據,然后將每個表添加到pdf表中的單個單元格中。 我的代碼如下。 我已經創建了select語句。 現在,我需要獲取每個語句並將它們放在各自的單元格中,因此about_me進入第2列第1行,教育進入第2列第2行,依此類推。
有任何想法嗎?
protected void pdfcreatorbtn_Click(object sender, EventArgs e)
{
//setup connection to SQL and retrieve data
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RIADDConnectionString"].ConnectionString);
SqlCommand aboutmecmd = new SqlCommand ("SELECT about_me FROM about_me",conn);
SqlCommand educationcmd = new SqlCommand ("SELECT education_name, details FROM education",conn);
SqlCommand employmentcmd = new SqlCommand ("SELECT job_name,details_emp,Year_started,year_finished FROM employment",conn);
SqlCommand hobbiescmd = new SqlCommand ("SELECT hobby_type,details_h FROM hobbies",conn);
SqlCommand skillscmd = new SqlCommand ("SELECT skill,rating FROM skills",conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
Document doc = new Document(iTextSharp.text.PageSize.A4, 10, 10, 42, 35);
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream("MatthewCremonaCV.PDF", FileMode.Create));
doc.Open(); //open document file
PdfPTable maintable = new PdfPTable(2); //declare new table
maintable.AddCell("image"); //col 1 row 1
maintable.AddCell(""); //col 2 row 1
maintable.AddCell("About Me"); //col 1 row 2
maintable.AddCell(""); //col 2 row 2
maintable.AddCell("Education"); //col 1 row 3
maintable.AddCell(""); //col 2 row 3
maintable.AddCell("Skills"); //col 1 row 4
maintable.AddCell(""); //col 2 row 4
maintable.AddCell("Employment History"); //col 1 row 5
maintable.AddCell(""); //col 2 row 5
maintable.AddCell("Hobies"); //col 1 row 6
maintable.AddCell(""); //col 2 row 6
maintable.AddCell("Contact"); //col 1 row 7
// maintable.AddCell("Mobile: 123456"\n"email:matthewriadd@gmail.com"); //col 2 row 7
doc.Add(maintable);
PdfPTable table = new datatable()
doc.Close();
您需要將結果集作為DataTable / DataSet返回。
var ds = new DataSet();
var da = new SqlDataAdapter(<command>);
da.Fill(ds);
混合該數據集后,就可以通過“表和行”屬性提取數據:
var aboutMe = ds.Tables[0].Rows[0]["about_me"];
請記住,返回的對象是一個對象,因此請確保將其轉換為適當的類型。
mainTable.AddCell(aboutMe.ToString());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.