使用选择查询显示PDF列时出错

时间:2014-04-14 10:38:04

标签: c# asp.net itextsharp

 PdfPTable tabl11 = new PdfPTable(8);
            string connect19 = ConfigurationManager.ConnectionStrings["ProjectConnectionString"].ConnectionString;
            SqlConnection cn19 = new SqlConnection(connect19);
            cn19.Open();
            SqlCommand cmd19 = new SqlCommand("select  distinct * from (select p1.PubID,p2.Publisher,p2.Title,p.name as authors from Personal_det p,Publication_Tracker p1,Publication_det p2 where p.FID=p1.FID and p1.Contribution_Type='A' and p1.PubID=p2.PubID  and p.FID=@FID ) t1 inner join (select p.Name as coauthors,p2.PubID,p2.Type,p2.Title,p2.PubDate,p2.Publisher from Personal_det p,Publication_Tracker p1,Publication_det p2 where p.FID=p1.FID and p1.Contribution_Type='C' and p1.PubID=p2.PubID and p1.PubID=4 ) t2 on t1.PubID = t2.PubID ", cn19);
            cmd19.Parameters.AddWithValue("@FID", Session["FID"]);
            SqlDataReader rdr11 = cmd19.ExecuteReader();
            if (!rdr11.Read())
            {
                tabl11.DeleteBodyRows();
                cn19.Close();
            }
            else
            {
                tabl11.SpacingBefore = 10f;
                bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
                times = new Font(bfTimes, 12f, Font.UNDERLINE);
                para = new Paragraph("Publication:", times);
                para.Alignment = Element.ALIGN_LEFT;
                doc.Add(para);
                cell.Colspan = 3;
                cell.Rowspan = 2;
                cell.HorizontalAlignment = 0;
                cell.BorderColor = new BaseColor(0, 0, 0);
                tabl11.AddCell(new Phrase("Pub ID", FontFactory.GetFont("Arial", 12, Font.BOLD, BaseColor.BLACK)));
                tabl11.AddCell(new Phrase("Publisher", FontFactory.GetFont("Arial", 12, Font.BOLD, BaseColor.BLACK)));
                tabl11.AddCell(new Phrase("Title", FontFactory.GetFont("Arial", 12, Font.BOLD, BaseColor.BLACK)));
                tabl11.AddCell(new Phrase("Authors", FontFactory.GetFont("Arial", 12, Font.BOLD, BaseColor.BLACK)));
                tabl11.AddCell(new Phrase("Co-Authors", FontFactory.GetFont("Arial", 12, Font.BOLD, BaseColor.BLACK)));
                tabl11.AddCell(new Phrase("Type", FontFactory.GetFont("Arial", 12, Font.BOLD, BaseColor.BLACK)));
                tabl11.AddCell(new Phrase("Title", FontFactory.GetFont("Arial", 12, Font.BOLD, BaseColor.BLACK)));
                tabl11.AddCell(new Phrase("Publication Date", FontFactory.GetFont("Arial", 12, Font.BOLD, BaseColor.BLACK)));

                string connect6 = ConfigurationManager.ConnectionStrings["ProjectConnectionString"].ConnectionString;
                using (SqlConnection cn = new SqlConnection(connect6))
                {
                    string query = "select * from (select p1.PubID,p2.Publisher,p2.Title,p.name as authors from Personal_det p,Publication_Tracker p1,Publication_det p2 where p.FID=p1.FID and p1.Contribution_Type='A' and p1.PubID=p2.PubID   ) t1 inner join (select p.Name as coauthors,p2.PubID,p2.Type,p2.Title,p2.PubDate from Personal_det p,Publication_Tracker p1,Publication_det p2 where p.FID=p1.FID and p1.Contribution_Type='C' and p1.PubID=p2.PubID  ) t2 on t1.PubID = t2.PubID ";
                    SqlCommand cmd = new SqlCommand(query, cn);
                    try
                    {
                        cn.Open();
                        using (SqlDataReader sdr = cmd.ExecuteReader())
                        {
                            while (sdr.Read())
                            {
                                tabl11.AddCell(new Phrase(sdr[0].ToString(), FontFactory.GetFont("Arial", 10, Font.NORMAL, BaseColor.BLACK)));
                                tabl11.AddCell(new Phrase(sdr[1].ToString(), FontFactory.GetFont("Arial", 10, Font.NORMAL, BaseColor.BLACK)));
                                tabl11.AddCell(new Phrase(sdr[2].ToString(), FontFactory.GetFont("Arial", 10, Font.NORMAL, BaseColor.BLACK)));
                                tabl11.AddCell(new Phrase(sdr[3].ToString(), FontFactory.GetFont("Arial", 10, Font.NORMAL, BaseColor.BLACK)));
                                tabl11.AddCell(new Phrase(sdr[4].ToString(), FontFactory.GetFont("Arial", 10, Font.NORMAL, BaseColor.BLACK)));
                                tabl11.AddCell(new Phrase(sdr[5].ToString(), FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)));
                                tabl11.AddCell(new Phrase(sdr[6].ToString(), FontFactory.GetFont("Arial", 10, Font.NORMAL, BaseColor.BLACK)));
                                tabl11.AddCell(new Phrase(Convert.ToDateTime(sdr[7]).ToShortDateString(), FontFactory.GetFont("Arial", 10, Font.NORMAL, BaseColor.BLACK)));

                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Response.Write(ex.Message);
                    }
                }

                cell.Rowspan = 2;
                cell.Colspan = 2;
                tabl11.AddCell(cell);
                tabl11.SpacingAfter = 30f;
                doc.Add(tabl11);
            }

以上代码用于显示 PDF 中的数据库列。但上面的查询我用过的是什么

select * from 
(select p1.PubID,p2.Publisher,p2.Title,p.name as authors 
from Personal_det p,Publication_Tracker p1,Publication_det p2 
where p.FID=p1.FID and p1.Contribution_Type='A' and p1.PubID=p2.PubID   ) t1 
inner join 
  (select p.Name as coauthors,p2.PubID,p2.Type,p2.Title,p2.PubDate 
   from Personal_det p,Publication_Tracker p1,Publication_det p2 
   where p.FID=p1.FID and p1.Contribution_Type='C' and p1.PubID=p2.PubID  ) t2 
   on t1.PubID = t2.PubID

四行,但在PDF中显示它时,它只显示one行。如何解决?任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

问题: 下面的查询只会得到7列,而不是8.因此会发生ArgumentOutOfRangeException。

select * from 
(select p1.PubID,p2.Publisher,p2.Title,p.name as authors 
from Personal_det p,Publication_Tracker p1,Publication_det p2 
where p.FID=p1.FID and p1.Contribution_Type='A' and p1.PubID=p2.PubID   ) t1 
inner join 
  (select p.Name as coauthors,p2.PubID,p2.Type,p2.Title,p2.PubDate 
   from Personal_det p,Publication_Tracker p1,Publication_det p2 
   where p.FID=p1.FID and p1.Contribution_Type='C' and p1.PubID=p2.PubID  ) t2 
   on t1.PubID = t2.PubID

答案:

select * from 
(select p1.PubID,p2.Publisher,p2.Title as firsttitle,p.name as authors 
from Personal_det p,Publication_Tracker p1,Publication_det p2 
where p.FID=p1.FID and p1.Contribution_Type='A' and p1.PubID=p2.PubID   ) t1 
inner join 
  (select p.Name as coauthors,p2.PubID,p2.Type,p2.Title,p2.PubDate 
   from Personal_det p,Publication_Tracker p1,Publication_det p2 
   where p.FID=p1.FID and p1.Contribution_Type='C' and p1.PubID=p2.PubID  ) t2 
   on t1.PubID = t2.PubID