读者在第一次结果后停止c#

时间:2016-07-20 14:02:10

标签: c# database

好的,所以我编写了代码来使用Itextsharp和select语句中的变量制作PDF文件。并更新到数据库中。读者必须使用我所做的查询返回多个结果。由于某种原因,它在第一个结果后停止。

任何人都可以告诉我需要更改的内容,使其遍历已返回的每一行并将其更新到while循环中的数据库。

这是我的代码。

    public Form1()
    {
        InitializeComponent();

    }

    private void CreateBtn_Click(object sender, EventArgs e)
    {

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

    }

    public void vaiabless()
    {



    }

    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 transtype = "";
        string breaker = "|";




        string connetionString = null;
        MySqlConnection cnn;
        connetionString = "*************";
        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 , accounting.transtype, 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 createcommand = new MySqlCommand(selectCmd, cnn);
            reader = createcommand.ExecuteReader();

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

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

                    transtype = reader["transtype"].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();





                    MemoryStream ms = new MemoryStream();
                    byte[] bits = new byte[0];

                    // Make The PDF File
                    Document NewDoc = new Document(iTextSharp.text.PageSize.A4, 0, 0, 0, 0);
                    PdfWriter pdfwri = PdfWriter.GetInstance(NewDoc,ms);

                    NewDoc.Open();

                    iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance("intsa-header.jpg");
                    img.ScaleAbsolute(596f, 100f); 


                    //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
                    #region pdftable

                    //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 + "  Total Due: " + Debit ));
                    //Body.HorizontalAlignment = Element.ALIGN_RIGHT;

                    //Caption.Colspan = 0;
                    //Caption.HorizontalAlignment = 1;
                    //General_Table.AddCell(Caption);
                    //General_Table.AddCell(Body);
                    PdfPTable mytable = new PdfPTable(3);
                    mytable.SpacingBefore = 40f;



                    Paragraph accountpar = new Paragraph("Description");
                    accountpar.IndentationLeft = 200f;
                    accountpar.SpacingBefore = 30f;
                    accountpar.SpacingAfter = 10f;

                    Paragraph Referencepar = new Paragraph( Reference);
                    Referencepar.IndentationLeft = 200f;
                    Referencepar.SpacingBefore = 30f;
                    Referencepar.SpacingAfter = 10f;

                    Paragraph Totalpar = new Paragraph("Total Due:" + "R" + Debit);
                    Totalpar.IndentationLeft = 200f;
                    Totalpar.SpacingBefore = 30f;
                    Totalpar.SpacingAfter = 10f;


                    Paragraph Refnumpar = new Paragraph("Reference Num:   "+refnum);
                    Refnumpar.IndentationLeft = 150f;
                    Refnumpar.SpacingBefore = 10f;
                    Refnumpar.SpacingAfter = 30f;


                    mytable.AddCell(Refnumpar);
                    mytable.AddCell(Referencepar);
                    mytable.AddCell(Totalpar);

                    #endregion


                    //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(accountpar);
                    //NewDoc.Add(Supscriptionpar);
                     NewDoc.Add(mytable);

                    //save Pdf
                    NewDoc.Close();
                    bits = ms.ToArray();






                    string updateCmd = "UPDATE users.accounting SET acc_pdf = @acc_pdf WHERE refnum =" + refnum;
                    MySqlConnection cnnx;
                    connetionString = "****************";
                    cnnx = new MySqlConnection(connetionString);
                    MySqlCommand Updatecommand = new MySqlCommand(updateCmd, cnnx);
                    Updatecommand.Parameters.AddWithValue("@acc_pdf", bits);
                   cnnx.Open();
                    Updatecommand.ExecuteNonQuery();

                    cnnx.Close();
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }



    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }
}

}

1 个答案:

答案 0 :(得分:2)

尝试更改msdn's example for the MySqlDataReader上提供的语句顺序:

 if (reader.HasRows)
    {
        while (reader.Read())
        {
            Console.WriteLine("{0}\t{1}", reader.GetInt32(0),
                reader.GetString(1));
        }
    }
    else
    {
        Console.WriteLine("No rows found.");
    }

还要检查@Cameron Tinker问的问题:

  

如果您在查询上运行查询,那么查询是否会返回多个结果   数据库直接?

为避免资源泄漏,请不要忘记使用reader.Close();以及连接关闭阅读器。 (或者更好地使用using关键字)