C# 为什么使用两个变量会导致查询返回时没有行
我有以下asp.net页面,该页面为中继器创建的每一行都有一个按钮:C# 为什么使用两个变量会导致查询返回时没有行,c#,asp.net,sql,C#,Asp.net,Sql,我有以下asp.net页面,该页面为中继器创建的每一行都有一个按钮: <asp:Repeater runat="server" ID="rptContent" OnItemCommand="btnGeneratePDF_Click"> <HeaderTemplate> <table border="0" style="width: 95%;"> <tr> <td
<asp:Repeater runat="server" ID="rptContent" OnItemCommand="btnGeneratePDF_Click">
<HeaderTemplate>
<table border="0" style="width: 95%;">
<tr>
<td style="width: 25%;">Name</td>
<td style="width: 25%;">Last Four SSN #</td>
<td style="width: 25%;">PDF Generator</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# Eval("name").ToString() %></td>
<td><%# Eval("ssn3").ToString() %></td>
<td><asp:Button ID="btnGeneratePDF" runat="server" Text="Generate PDF" CommandArgument='<%# Eval("name").ToString() + ", " + Eval("ssn3").ToString() %>' /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
如果我的
sqlCode
是sqlCode=“从[DSPCONTENT01].[dbo].[TablePDFTest]中选择*,其中[name]=”+k+”//而且[ssn3]='“+c+””;
对于formFieldMap
但是如果我的
sqlCode
是sqlCode=“从[DSPCONTENT01].[dbo].[TablePDFTest]中选择*,其中[name]='“+k+”,[ssn3]='“+c+”;
表单字段映射无法正常工作
这是中继器显示内容的示例:
我怎样才能修好它
更新:
我使用MessageBox
进行了一次测试,以使用带有两个变量的查询来显示值:
public void writeData(string k, string c)
{
Conn = new SqlConnection(cString);
Conn.Open();
//MessageBox.Show(k);
//MessageBox.Show(c);
nameE = txtName.Text;
var pdfPath = Path.Combine(Server.MapPath("~/PDFTemplates/fw9.pdf"));
// Get the form fields for this PDF and fill them in!
var formFieldMap = PDFHelper.GetFormFieldNames(pdfPath);
formFieldMap["topmostSubform[0].Page1[0].f1_01_0_[0]"] = k;
sqlCode = "SELECT * FROM [db].[dbo].[TablePDFTest] WHERE [name] = '" + k + "' AND [ssn3] = '" + c + "'";
//MessageBox.Show("" + sqlCode.ToString());
using (SqlCommand command = new SqlCommand(sqlCode, Conn))
{
command.CommandType = CommandType.Text;
using (reader = command.ExecuteReader())
{
if (reader.HasRows)
{
if (reader.Read())
{
MessageBox.Show(reader.GetValue(1).ToString());
MessageBox.Show(reader.GetValue(2).ToString());
MessageBox.Show(reader.GetValue(3).ToString());
MessageBox.Show(reader.GetValue(4).ToString());
MessageBox.Show(reader.GetValue(5).ToString());
MessageBox.Show(reader.GetValue(6).ToString());
MessageBox.Show(reader.GetValue(7).ToString());
MessageBox.Show(reader.GetValue(8).ToString());
MessageBox.Show(reader.GetValue(9).ToString());
/*formFieldMap["topmostSubform[0].Page1[0].f1_02_0_[0]"] = reader.GetValue(1).ToString();
formFieldMap["topmostSubform[0].Page1[0].f1_04_0_[0]"] = reader.GetValue(2).ToString();
formFieldMap["topmostSubform[0].Page1[0].f1_05_0_[0]"] = reader.GetValue(3).ToString();
formFieldMap["topmostSubform[0].Page1[0].f1_07_0_[0]"] = reader.GetValue(4).ToString();
formFieldMap["topmostSubform[0].Page1[0].social[0].TextField1[0]"] = reader.GetValue(5).ToString();
formFieldMap["topmostSubform[0].Page1[0].social[0].TextField2[0]"] = reader.GetValue(6).ToString();
formFieldMap["topmostSubform[0].Page1[0].social[0].TextField2[1]"] = reader.GetValue(7).ToString();
formFieldMap["topmostSubform[0].Page1[0].social[0].TextField2[2]"] = reader.GetValue(8).ToString();
formFieldMap["topmostSubform[0].Page1[0].social[0].TextField2[3]"] = reader.GetValue(9).ToString();*/
}
}
}
}
// Requester's name and address (hard-coded)
/*formFieldMap["topmostSubform[0].Page1[0].f1_06_0_[0]"] = "Medical Group\n27 West Ave\nPurchase, NY 10577";
var pdfContents = PDFHelper.GeneratePDF(pdfPath, formFieldMap);
PDFHelper.ReturnPDF(pdfContents, "Completed-W9.pdf");*/
}
当我点击按钮时,消息框不再显示,什么也没有发生。可能是您应该使用一些ORM系统,这将使您的数据库生活更轻松。它还将帮助您避免一些sql注入,这些都是您在代码中已经做过的
尝试在google上搜索Fluent NHibernateSQL中
ssn3
的数据类型是什么?您将c
作为字符串文本传递。这是一个值得注意的问题。也许可以尝试删除c值周围的单引号(例如,如果它是整数)
另外,当您使用WHERE子句的一些测试数据将查询输入查询分析器时,会发生什么情况?您知道是否应该有与这两个值匹配的行吗?SQL注入任何人?使用参数化查询。这是我在查询正常工作后的下一步:)它仍在开发中,所以我现在不担心谢谢你的指点。你能定义“formFieldMap”不能正常工作吗?你有错误吗?你没有得到任何返回的结果吗?其次,如果你调用这个方法并在“sqlCode”上设置断点,并对字符串执行快速监视,其值是多少?我认为,测试您不打算使用的代码是一种很好的方法,可以确保您修复的错误超过需要的数量。第一次编写正确的代码可以省去你的麻烦。这个问题有点难理解,但我想确认它是因为和,可能没有匹配两个变量的记录。这可能是个好建议,但它无论如何都不是问题的答案。它是
varchar
。我更新了代码以使用Messagebox进行测试。当我点击任何一个按钮时,什么都不会发生。我想我的问题不知怎么搞砸了?更正,你是对的。我移除了c
周围的'
,它就工作了。不知道为什么,因为它不是表中的typeint
。我猜有一大堆隐式类型转换正在进行。如果使用参数化查询(这对于安全性来说是非常必要的),那么这种混乱会得到更好的解决。使用此代码,您将面临SQL注入攻击。至少你应该确保应用程序标识只有data reader才能访问数据库,否则你可能会发现你的数据库被破坏或修改,从而将恶意脚本喷回你的网站。谢谢你的提醒。我能够转换一个只使用一个变量的查询,但不确定在这种情况下如何使用多个变量的参数。没问题。如果您在使用参数时遇到困难,请进行一些研究,尽最大努力,如果遇到困难,请在新问题中发布代码。
public void writeData(string k, string c)
{
Conn = new SqlConnection(cString);
Conn.Open();
//MessageBox.Show(k);
//MessageBox.Show(c);
nameE = txtName.Text;
var pdfPath = Path.Combine(Server.MapPath("~/PDFTemplates/fw9.pdf"));
// Get the form fields for this PDF and fill them in!
var formFieldMap = PDFHelper.GetFormFieldNames(pdfPath);
formFieldMap["topmostSubform[0].Page1[0].f1_01_0_[0]"] = k;
sqlCode = "SELECT * FROM [db].[dbo].[TablePDFTest] WHERE [name] = '" + k + "' AND [ssn3] = '" + c + "'";
//MessageBox.Show("" + sqlCode.ToString());
using (SqlCommand command = new SqlCommand(sqlCode, Conn))
{
command.CommandType = CommandType.Text;
using (reader = command.ExecuteReader())
{
if (reader.HasRows)
{
if (reader.Read())
{
MessageBox.Show(reader.GetValue(1).ToString());
MessageBox.Show(reader.GetValue(2).ToString());
MessageBox.Show(reader.GetValue(3).ToString());
MessageBox.Show(reader.GetValue(4).ToString());
MessageBox.Show(reader.GetValue(5).ToString());
MessageBox.Show(reader.GetValue(6).ToString());
MessageBox.Show(reader.GetValue(7).ToString());
MessageBox.Show(reader.GetValue(8).ToString());
MessageBox.Show(reader.GetValue(9).ToString());
/*formFieldMap["topmostSubform[0].Page1[0].f1_02_0_[0]"] = reader.GetValue(1).ToString();
formFieldMap["topmostSubform[0].Page1[0].f1_04_0_[0]"] = reader.GetValue(2).ToString();
formFieldMap["topmostSubform[0].Page1[0].f1_05_0_[0]"] = reader.GetValue(3).ToString();
formFieldMap["topmostSubform[0].Page1[0].f1_07_0_[0]"] = reader.GetValue(4).ToString();
formFieldMap["topmostSubform[0].Page1[0].social[0].TextField1[0]"] = reader.GetValue(5).ToString();
formFieldMap["topmostSubform[0].Page1[0].social[0].TextField2[0]"] = reader.GetValue(6).ToString();
formFieldMap["topmostSubform[0].Page1[0].social[0].TextField2[1]"] = reader.GetValue(7).ToString();
formFieldMap["topmostSubform[0].Page1[0].social[0].TextField2[2]"] = reader.GetValue(8).ToString();
formFieldMap["topmostSubform[0].Page1[0].social[0].TextField2[3]"] = reader.GetValue(9).ToString();*/
}
}
}
}
// Requester's name and address (hard-coded)
/*formFieldMap["topmostSubform[0].Page1[0].f1_06_0_[0]"] = "Medical Group\n27 West Ave\nPurchase, NY 10577";
var pdfContents = PDFHelper.GeneratePDF(pdfPath, formFieldMap);
PDFHelper.ReturnPDF(pdfContents, "Completed-W9.pdf");*/
}