2014-04-14 24 views
0
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 [email protected]) 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顯示數據庫中的列。但我所使用錯誤,同時使用選擇查詢顯示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行。如何解決?任何幫助讚賞。

+2

行'PdfPTable tabl11 =新PdfPTable(8);'創建一個8列的表格,但您的評論說,該數據庫有4列。此外,你說「四列,但在PDF中顯示時只顯示一行」。列數不影響行數,您需要更好地解釋問題。什麼是「單元」變量。請通過刪除不屬於問題一部分的代碼(單元格,段落,時間,FontFactory等)來更新上述內容。另外,因爲我們沒有數據庫,所以如果你給了我們一些像數組數組一樣可重複的東西,它將會有所幫助。 –

+0

您不關閉sql連接cn19和SqlDataReader rdr11。此外,這兩個查詢在條件有所不同。這有效嗎? – ray

+0

或者您可以創建一個sql小提琴http://sqlfiddle.com/來幫助我們瞭解您的問題 –

回答

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