该命令不会将来自idagent的值替换为mysql查询中的@idagent。任何想法为什么?我试图从.txt文件中获取id(并且它正在工作,因为我将exe值放在另一个.txt中,并且它们显示正确)并将它们用作mysql命令的id。
编辑:实际上,我认为该参数是替换但该表不包含任何数据。当我尝试在result.txt中输出name变量时,它显示为空白。
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.xml;
using System.IO;
using MySql.Data.MySqlClient;
namespace PdfGenerator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void Form1_Load(object sender, EventArgs e)
{
FillForm();
ListFieldNames();
}
/// <summary>
/// List all of the form fields into a textbox. The
/// form fields identified can be used to map each of the
/// fields in a PDF.
/// </summary>
public void ListFieldNames()
{
string pdfTemplate = @"C:\xampp\htdocs\site\pdfgen\PdfGenerator\form.pdf";
// title the form
this.Text += " - " + pdfTemplate;
// create a new PDF reader based on the PDF template document
PdfReader pdfReader = new PdfReader(pdfTemplate);
// create and populate a string builder with each of the
// field names available in the subject PDF
StringBuilder sb = new StringBuilder();
foreach (DictionaryEntry de in pdfReader.AcroFields.Fields)
{
sb.Append(de.Key.ToString() + Environment.NewLine);
}
// Write the string builder's content to the form's textbox
textBox1.Text = sb.ToString();
textBox1.SelectionStart = 0;
}
public void FillForm()
{
string idagent = null;
string idfirma = null;
string[] lines = System.IO.File.ReadAllLines(@"C:\xampp\htdocs\site\var.txt");
idfirma = lines[0];
idagent = lines[1];
using (var connection = new MySqlConnection("server = localhost; User Id = root; password = ; database = documente;"))
{
connection.Open();
string agenti = "SELECT * FROM `agenti` WHERE `ID`=@idagent";
string nume = null;
string telefon = null;
string sigiliu = null;
string legitimatie = null;
string prefix = null;
using (var cmd = new MySqlCommand(agenti, connection))
{
cmd.Parameters.Add(new MySqlParameter("@idagent", idagent));
System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\xampp\htdocs\site\result.txt");
file.WriteLine(idfirma);
file.WriteLine(idagent);
file.WriteLine(cmd);
file.Close();
MySqlDataReader rdr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(rdr);
foreach (DataRow row in dt.Rows)
{
nume = row["Nume Agent"].ToString();
telefon = row["Telefon"].ToString();
sigiliu = row["Sigiliu"].ToString();
legitimatie = row["Legitimatie"].ToString();
prefix = row["Prefix"].ToString();
}
string pdfTemplate = @"C:\xampp\htdocs\site\pdfgen\PdfGenerator\form.pdf";
string newFile = @"C:\xampp\htdocs\site\pdfgen\PdfGenerator\completed_form.pdf";
PdfReader pdfReader = new PdfReader(pdfTemplate);
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(
newFile, FileMode.Create));
AcroFields pdfFormFields = pdfStamper.AcroFields;
// set form pdfFormFields
// The first worksheet and W-4 form
pdfFormFields.SetField("topmostSubform[0].Page1[0].numeagent[0]", nume);
pdfFormFields.SetField("topmostSubform[0].Page1[0].nrpv[0]", prefix);
pdfFormFields.SetField("topmostSubform[0].Page1[0].telefonagent[0]]", telefon);
// flatten the form to remove editting options, set it to false
// to leave the form open to subsequent manual edits
pdfStamper.FormFlattening = true;
// close the pdf
pdfStamper.Close();
Application.Exit();
}
}
}
}
}