读取器在第一个结果c#之后停止

本文关键字:之后 结果 第一个 读取 | 更新日期: 2023-09-27 17:57:56

好的,所以我已经编写了使用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)
    {
    }
}

}

读取器在第一个结果c#之后停止

尝试更改语句的顺序,如msdn的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关键字)