gpt4 book ai didi

c# - 从一个选择查询中获取多个结果

转载 作者:行者123 更新时间:2023-11-29 20:47:44 25 4
gpt4 key购买 nike

所以我被我的代码困住了,并彻底尝试寻找答案。

我有 1 个 select 语句,产生了 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);
}

最佳答案

问题是您在 while 循环中创建 PDF 文件,我认为您不需要 52k PDF 文件,所以您应该首先创建 PDF 文件,然后循环所有结果并将它们粘贴到文件中.

编辑:我建议将行添加到 insert/update PDF to database之后

NewDoc.Close();

然后删除本地文件(text.pdf),然后再次运行循环。

关于c# - 从一个选择查询中获取多个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38323804/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com