2016-07-12 62 views
0

所以我堅持我的代碼,並徹底地嘗試尋找答案。從一個選擇查詢獲取多個結果

我有1個選擇語句,帶來了52806個結果。

我把查詢的結果放在變量中運行,然後把它放到我製作的PDF文件中。第一個結果後,它不起作用。我想讓它將結果放在我的pdf文件中,然後轉到下一個結果。

如果任何人都可以幫助我身份證讚賞了很多。

這是我的代碼。提前對不良做法感到抱歉。

private void CreateBtn_Click(object sender, EventArgs e) 
    { 

     try 
     { 
      make_pdf(); 
     } 
     catch (Exception exe) 
     { 
      MessageBox.Show("failed"); 
     } 

    } 

    public static void make_pdf() 
     { 
     string Contact = ""; 
     string emailAddress = ""; 
     string Tel = ""; 
     string InvoiceDate = ""; 
     string address = ""; 
     string Reference = ""; 
     string AccountNo = ""; 
     string Debit = ""; 
     string Credit = ""; 
     string refnum = ""; 





     string connetionString = null; 
     MySqlConnection cnn; 
     connetionString = "server=********;user id=web_support;database=users;password=!w3b_supp0rt~;persistsecurityinfo=True"; 
     cnn = new MySqlConnection(connetionString); 
     try 
     { 
      cnn.Open(); 
      // MessageBox.Show("Connection Open ! "); 

     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("Can not open connection ! "); 
     } 


     try 
     { 
      MySqlDataReader reader = null; 
      string selectCmd = "SELECT accounting.code,users.curr_email , users.physicaddr ,accounting.date , accounting.refnum , users.telephone , accounting.debit , accounting.acc_pdf, accounting.credit, accounting.reference, users.contact, accounting.transnum FROM accounting INNER JOIN users ON accounting.code = users.code WHERE(accounting.transtype = 1)"; 

      MySqlCommand command = new MySqlCommand(selectCmd, cnn); 
      reader = command.ExecuteReader(); 

      while (reader.Read()) 
      { 
       if (reader.HasRows) 
       { 

        //get account number 
        AccountNo = reader["code"].ToString(); 
        //get emailaddress 
        emailAddress = reader["curr_email"].ToString(); 

        //get Contact Name 
        Contact = reader["contact"].ToString(); 

        //get telephone number 
        Tel = reader["telephone"].ToString(); 

        //Get Date 
        InvoiceDate = reader["date"].ToString(); 

        //Get reference 
        Reference = reader["reference"].ToString(); 

        //Get Address 
        address = reader["physicaddr"].ToString(); 

        //Get Debit 
        Debit = reader["debit"].ToString(); 

        //Get Credit 
        Credit = reader["credit"].ToString(); 

        //Get Refnum 
        refnum = reader["refnum"].ToString(); 


        // MessageBox.Show(address+" "+Reference+" "+InvoiceDate+" "+emailAddress+" "+AccountNo+" "+Contact); 


        // Make The PDF File 
        Document NewDoc = new Document(iTextSharp.text.PageSize.A4, 0, 0, 0, 0); 
        PdfWriter pdfwri = PdfWriter.GetInstance(NewDoc, new FileStream("text.pdf", FileMode.Create)); 

        NewDoc.Open(); 

        iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance("intsa_header_small.jpg"); 
        // Paragraph par = new Paragraph("Everything is working"); 

        //Account List 
        List AccountNolist = new List(List.UNORDERED); 
        AccountNolist.SetListSymbol(""); 
        AccountNolist.IndentationLeft = 300f; 
        AccountNolist.Add(new ListItem("AccountNo " + AccountNo)); 

        // AddressList 
        List AddressList = new List(List.UNORDERED); 
        AddressList.SetListSymbol(""); 
        AddressList.IndentationLeft = 300f; 
        AddressList.Add(new ListItem("Address: " + address)); 
        #region Emailaddresslist 
        //EmailAddressList 
        List emailAddresslist = new List(List.UNORDERED); 
        emailAddresslist.SetListSymbol(""); 
        emailAddresslist.IndentationLeft = 300f; 
        emailAddresslist.Add(new ListItem("Email address: " + emailAddress)); 
        #endregion 
        //ContactList 
        List Contactlist = new List(List.UNORDERED); 
        Contactlist.SetListSymbol(""); 
        Contactlist.IndentationLeft = 300f; 
        Contactlist.Add(new ListItem("Contact: " + Contact)); 

        //TelephoneList 
        List Telephonelist = new List(List.UNORDERED); 
        Telephonelist.SetListSymbol(""); 
        Telephonelist.IndentationLeft = 300f; 
        Telephonelist.Add(new ListItem("Tel: " + Tel)); 

        // Make a Table 


        PdfPTable General_Table = new PdfPTable(1); 
        General_Table.SpacingBefore = 50f; 
        General_Table.SpacingAfter = 50f; 

        PdfPCell Caption = new PdfPCell(new Phrase("Description")); 

        PdfPCell Body = new PdfPCell(new Phrase("  " + refnum + " " + Reference + " Debit: " + Debit + " Credit: " + Credit)); 
        Body.HorizontalAlignment = Element.ALIGN_RIGHT; 

        Caption.Colspan = 0; 
        Caption.HorizontalAlignment = 1; 
        General_Table.AddCell(Caption); 
        General_Table.AddCell(Body); 


        // NewDoc.Add(par); 

        //add Image to pdf 
        NewDoc.Add(img); 
        //add accountNo to pdf 
        NewDoc.Add(AccountNolist); 
        //add Contact to pdf 
        NewDoc.Add(Contactlist); 

        //add emailaddress to pdf 
        NewDoc.Add(emailAddresslist); 
        //add Telephone Number to pdf 
        NewDoc.Add(Telephonelist); 

        //add address to pdf 
        NewDoc.Add(AddressList); 

        NewDoc.Add(General_Table); 

        //save Pdf 
        NewDoc.Close(); 




       } 
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
+0

您從連接字符串中刪除了服務器名稱,並忘記了密碼。恭喜!現在黑客只需要找到服務器,他們已經擁有了憑據。你最好在太晚之前更改密碼。 –

+0

好,如果他們能連接到它是我的客人。儘管運氣很好,但服務器正在脫機運行。 –

+0

黑客可以來自組織內部,他們已經知道服務器在哪裏。除非您是唯一有權訪問的人員,或者其他人已經擁有對數據庫的完全訪問權限。 –

回答

0

是要創建while循環的PDF文件中的問題,我不認爲你通過所有的結果和粘貼的希望52K PDF文件,所以你應該首先創建的PDF文件,然後循環他們進入文件。

編輯:我會建議增加線insert/update PDF to database

NewDoc.Close(); 

然後刪除重新運行循環之前的本地文件(text.pdf)。

+0

嗨hynek感謝您的答覆。生病了試一試。但我不得不說,我實際上是這樣做使得52k pdf文件進入數據庫。 –

+0

那麼你應該在while循環中留下pdf文件的創建,但我注意到你有pdf文件的靜態名稱,如果你想要多個文件,你需要改變每個文件的名稱(可能是通過自動增加值或聯繫人的姓名) –

+0

好吧,我明白你在說什麼。你認爲我可以通過在每個結果之後將pdf文件插入數據庫來制定解決方法,然後轉到下一個結果?因爲我想用查詢結果生成pdf文件 - 將其更新到數據庫中,然後轉到下一個結果。或者那會太多? –

相關問題