C# 参数.Addwith不替换
该命令没有将来自idagent的值替换为mysql查询中的@idagent。有什么想法吗?为什么?我正试图从一个.txt文件中获取id(之所以有效,是因为我将exe获取的值放在另一个.txt文件中,它们显示正确),并将它们用作mysql命令的id 编辑:实际上,我认为参数是replace,但表中没有数据。当我尝试在my result.txt中输出name变量时,它显示为空C# 参数.Addwith不替换,c#,mysql,C#,Mysql,该命令没有将来自idagent的值替换为mysql查询中的@idagent。有什么想法吗?为什么?我正试图从一个.txt文件中获取id(之所以有效,是因为我将exe获取的值放在另一个.txt文件中,它们显示正确),并将它们用作mysql命令的id 编辑:实际上,我认为参数是replace,但表中没有数据。当我尝试在my result.txt中输出name变量时,它显示为空 using System; using System.Collections; using System.Component
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();
}
}
}
}
}
使用系统;
使用系统集合;
使用系统组件模型;
使用系统数据;
使用系统图;
使用系统文本;
使用System.Windows.Forms;
使用iTextSharp;
使用iTextSharp.text;
使用iTextSharp.text.pdf;
使用iTextSharp.text.xml;
使用System.IO;
使用MySql.Data.MySqlClient;
命名空间PdfGenerator
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
public void Form1_Load(对象发送方、事件参数e)
{
FillForm();
ListFieldNames();
}
///
///在文本框中列出所有表单字段
///标识的表单字段可用于映射每个
///PDF中的字段。
///
public void ListFieldNames()
{
字符串pdfTemplate=@“C:\xampp\htdocs\site\pdfgen\PdfGenerator\form.pdf”;
//标题表格
此.Text+=“-”+pdfTemplate;
//基于PDF模板文档创建新的PDF阅读器
PdfReader PdfReader=新PdfReader(pdfTemplate);
//使用以下各项创建并填充字符串生成器:
//主题PDF中提供的字段名称
StringBuilder sb=新的StringBuilder();
foreach(pdfReader.AcroFields.Fields中的DictionaryEntry de)
{
sb.Append(de.Key.ToString()+Environment.NewLine);
}
//将字符串生成器的内容写入表单的文本框
textBox1.Text=sb.ToString();
textBox1.SelectionStart=0;
}
公共表格()
{
字符串idagent=null;
字符串idfirma=null;
string[]lines=System.IO.File.ReadAllLines(@“C:\xampp\htdocs\site\var.txt”);
idfirma=行[0];
idagent=行[1];
使用(var connection=newmysqlconnection(“服务器=localhost;用户Id=root;密码=;数据库=documente;”)
{
connection.Open();
string agenti=“从`agenti`中选择*,其中`ID`=@idagent”;
字符串nume=null;
字符串telefon=null;
字符串sigiliu=null;
字符串合法性=null;
字符串前缀=null;
使用(var cmd=newmysqlcommand(agenti,connection))
{
cmd.Parameters.Add(新的MySqlParameter(“@idagent”,idagent));
System.IO.StreamWriter file=new System.IO.StreamWriter(@“C:\xampp\htdocs\site\result.txt”);
file.WriteLine(idfirma);
文件写入线(idagent);
file.WriteLine(cmd);
file.Close();
MySqlDataReader rdr=cmd.ExecuteReader();
DataTable dt=新的DataTable();
dt.荷载(rdr);
foreach(数据行中的数据行)
{
nume=行[“nume代理”].ToString();
telefon=行[“telefon”]。ToString();
sigiliu=行[“sigiliu”].ToString();
合法性=行[“合法性”].ToString();
prefix=行[“prefix”]。ToString();
}
字符串pdfTemplate=@“C:\xampp\htdocs\site\pdfgen\PdfGenerator\form.pdf”;
字符串newFile=@“C:\xampp\htdocs\site\pdfgen\PdfGenerator\completed_form.pdf”;
PdfReader PdfReader=新PdfReader(pdfTemplate);
PdfStamper PdfStamper=新的PdfStamper(pdfReader,新文件流(
newFile,FileMode.Create));
AcroFields pdfFormFields=pdfStamper.AcroFields;
//设置表单pdfFormFields
//第一份工作表和W-4表格
pdfFormFields.SetField(“topmostSubform[0].Page1[0].numeagent[0]”,nume);
pdfFormFields.SetField(“topmostSubform[0].Page1[0].nrpv[0]”,前缀);
pdfFormFields.SetField(“topmostSubform[0].Page1[0].telefonagent[0]]”,telefon);
//展平表单以删除编辑选项,将其设置为false
//将表单保留为打开状态以供后续手动编辑
pdfStamper.formflatting=true;
//关闭pdf
pdfStamper.Close();
Application.Exit();
}
}
}
}
}
您是否尝试过这样添加参数:cmd.parameters.Add(“@idagent”,SqlDbType.VarChar)。Value=idagent代码>?@Nino我想我的表有问题,因为我试图为每个表添加file.Writeline,但它们什么也没有显示。有什么想法吗?为什么?我想这个命令可以替换,但是由于数据表是空的,我不能在我的pdf文件上打印任何内容。在.txt文件中,它只是不写任何东西。