C# 靠近'的语法不正确';字符串';后的未闭合引号';
我在VisualWebDev2010Express中使用C#创建了一个Web表单,它包含文本框/标签(基于复选框),但当我运行代码时,运行SQL命令失败。在只使用3个文本框之前,我已经让代码正常工作了,但是当我扩展代码时,它似乎不起作用 我尝试过的事情: 谷歌搜索时,有人说引号是个问题(错误反映了这一点),但如果我从SQL命令中删除所有引号,它就会失败,但我的想法是它总是必须是C# 靠近'的语法不正确';字符串';后的未闭合引号';,c#,asp.net,sql-server,visual-studio-2010,C#,Asp.net,Sql Server,Visual Studio 2010,我在VisualWebDev2010Express中使用C#创建了一个Web表单,它包含文本框/标签(基于复选框),但当我运行代码时,运行SQL命令失败。在只使用3个文本框之前,我已经让代码正常工作了,但是当我扩展代码时,它似乎不起作用 我尝试过的事情: 谷歌搜索时,有人说引号是个问题(错误反映了这一点),但如果我从SQL命令中删除所有引号,它就会失败,但我的想法是它总是必须是“+datahere+”,这是真的吗 我还将在下面的错误中引用有关web表单的所有代码。运行“调试”时,错误如下所示:
“+datahere+”
,这是真的吗
我还将在下面的错误中引用有关web表单的所有代码。运行“调试”时,错误如下所示:
“t”附近的语法不正确。
字符串“')”后的未闭合引号
描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源
异常详细信息:System.Data.SqlClient.SqlException:“t”附近的语法不正确。
字符串“')”后的未闭合引号
源错误:
Line 40: }
Line 41: SqlCommand cmd = new SqlCommand("insert into sicknesstest values('"+txtname.Text+"','"+txtgrade.Text+"','"+txtdepot.Text+"','"+txtillness.Text+"','"+txtabsence.Text+"','"+txtcontactby.Text+"','"+txtupdate.Text+"','"+txtdetails.Text+"','"+txtresumedate.Text+"','"+txtdetail.Text+"','"+txtmedonreturn.Text+"','"+txtreporter.Text+"','"+txtdateofcontact.Text+"','"+txtresumeddate.Text+"')", con);
Line 42: cmd.ExecuteNonQuery();
Line 43: con.Close();
Line 44: Label1.Visible = true;
源文件:C:\Users\MYNAME\Documents\Visual Studio 2010\Projects\SicknessDBNewSite\SicknessDBNewSite\WebForm2.aspx.cs行:42
堆栈跟踪:
[SqlException (0x80131904): Incorrect syntax near 't'.
Unclosed quotation mark after the character string ')'.]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +1791910
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +5347106
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +546
System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +1693
System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite) +869
System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite) +413
System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +163
SicknessDBNewSite.WebForm2.Button1_Click(Object sender, EventArgs e) in C:\Users\MYNAME\Documents\Visual Studio 2010\Projects\SicknessDBNewSite\SicknessDBNewSite\WebForm2.aspx.cs:42
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9653178
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +103
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724
代码隐藏: webform2.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="SicknessDBNewSite.WebForm2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 72%;
}
.style2
{
width: 443px;
}
.style3
{
text-align: center;
text-decoration: underline;
}
.style4
{
width: 443px;
height: 65px;
}
.style5
{
height: 65px;
}
.style6
{
text-align: center;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align: center">
<h1 class="style3">
Test Form Submission
</h1>
<table class="style1" align="center">
<tr>
<td class="style2">
Name</td>
<td>
<asp:TextBox ID="txtname" runat="server" Width="500px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
Grade</td>
<td>
<asp:TextBox ID="txtgrade" runat="server" Width="500px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
Depot</td>
<td>
<asp:TextBox ID="txtdepot" runat="server" Width="500px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
Nature of Illness?</td>
<td>
<asp:TextBox ID="txtillness" runat="server" Width="500px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
Appointment made to see doctor (Yes or No)?</td>
<td>
<asp:RadioButton ID="RadioAppointmentYes" runat="server" Text="Yes" />
<asp:RadioButton ID="RadioAppointmentNo" runat="server" Text="No" />
<asp:Label ID="LabelAppointment" runat="server" Visible="False"></asp:Label>
</td>
</tr>
<tr>
<td class="style2">
Medication Taken (Yes or No)?</td>
<td class="style6">
<asp:RadioButton ID="RadioMedicationYes" runat="server" Text="Yes" />
<asp:RadioButton ID="RadioMedicationNo" runat="server" Text="No" />
<asp:Label ID="LabelMedication" runat="server" Visible="False"></asp:Label>
</td>
</tr>
<tr>
<td class="style2">
Expected length of absence?</td>
<td>
<asp:TextBox ID="txtabsence" runat="server" Width="500px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
If not contacted by staff member, who contacted on their behalf?</td>
<td>
<asp:TextBox ID="txtcontactby" runat="server" Width="500px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
Likely resumption date?</td>
<td>
<asp:TextBox ID="txtresumedate" runat="server" Width="500px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
Contact telephone number</td>
<td>
<asp:TextBox ID="txtcontactnum" runat="server" Width="500px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
Latest update date</td>
<td>
<asp:TextBox ID="txtupdate" runat="server" Width="500px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style4">
Details</td>
<td class="style5">
<asp:TextBox ID="txtdetails" runat="server" Width="500px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
Resumption Date</td>
<td>
<asp:TextBox ID="txtresumeddate" runat="server" Width="500px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
Detail</td>
<td>
<asp:TextBox ID="txtdetail" runat="server" Width="500px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
Is the person still on medication at the time of resumption?</td>
<td>
<asp:TextBox ID="txtmedonreturn" runat="server" Width="500px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
Controller / Planner's name</td>
<td>
<asp:TextBox ID="txtreporter" runat="server" Width="500px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
Date of contact</td>
<td>
<asp:TextBox ID="txtdateofcontact" runat="server" Width="500px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
Date and time of next shift</td>
<td>
<asp:TextBox ID="txtdatetimeshift" runat="server" Width="500px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
</td>
<td>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" Width="100px" />
</td>
</tr>
</table>
</div>
<a href="http://INTRANET/webform1.aspx">
<asp:Label ID="Label1" runat="server" ForeColor="Green" Visible="False" /></asp:Label>
</a>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionStringTestFormSubmission %>"
SelectCommand="SELECT * FROM [sicknesstest]"></asp:SqlDataSource>
</form>
</body>
</html>
--
日期来源:
网络配置:
该数据库在我的机器上作为“测试台”本地运行,并在我所能看到的范围内进行查找
<add name="ConnectionStringTestFormSubmission" connectionString="Data Source=G00069-PC\SQLEXPRESS;Initial Catalog=SicknessDatabase;Persist Security Info=True;User ID=REMOVED;Password=REMOVED"
providerName="System.Data.SqlClient" />
92.7%(a)如果在尝试执行之前输出字符串,那么所有SQL执行问题都会变得明显
很可能您的输入字段中有一个错误的报价,不一定是恶意的-它可能是由哦,不,我们不会去或代码不起作用
或联系O'Rourke先生
引起的
这意味着做一些类似于:
string strcmd = "insert into ...";
// Examine/output strcmd here.
SqlCommand cmd = new SqlCommand(strcmd);
cmd.ExecuteNonQuery();
然后,一旦您让它工作起来,就可以查找两个术语“SQL注入攻击”和“SQL准备语句”。在某个时候,您会希望开始使用后者来防止前者
(a) 从稀薄的空气中拉出的数字,实际值可能会有所不同:-)尝试重新构建您的语句。在c#中,我永远不会使用这样的字符串,请使用String.Format
。通常,由于SQL注入,您甚至不应该以这种方式将参数传递给数据库。尝试使用SqlParameter
我认为出现问题的原因是某些文本框中的值错误,在使用SqlParameter
时不会发生这种情况
您应该阅读这篇文章,然后回答:我的建议是在查询中使用参数,而不是串联。sql查询中使用参数的示例如下:
IDbCommand command = conn.CreateCommand();
command.CommandText = "SELECT * FROM M_ITEM_SERIAL WHERE ITSR_PLANT_ID = @plantId";
IDbDataParameter plantIdParam = command.CreateParameter();
plantIdParam.ParameterName = "@plantId";
plantIdParam.Direction = ParameterDirection.Input;
plantIdParam.Value = plantId;
command.Parameters.Add(plantIdParam);
这是一个SQL错误。请从问题中删除所有不相关的代码。查找准备好的语句,这将使您更加轻松,如果您相应地更改代码,错误可能会消失。发现问题,似乎与我的文本框中的“was not”导致SQL终止(SQL注入?)有关删除它解决了这个问题,但现在又出现了另一个错误:(字符串或二进制数据将被截断。语句已被终止。
Jamie,另一个问题是另一个问题的充分理由,就是这样。建议你完成这个问题(尽管你觉得最有帮助,但向上投票/接受)然后把它当作一个新问题来问。如果你改变了一个问题的本质,那么所有的答案都会变得毫无用处。@RhysJones你的第二句话是正确的。你的第一句话错得太多了。谢谢你,我会试试看:),网站或视频上有什么建议可以尝试学习这类东西吗?正如目前所学的那样,我认为MSDN是一个很好的起点。但是youtube上有很多关于ADO.net的视频。@JamieB好吧,你从哪里学到的SqlCommand
?很有可能它还提到了参数化。。。如果不是:(不寒而栗)@MarcGravel这是通过Youtube进行的,因为我无法理解人们正在编写的所有代码,反向设计了一个Youtube教程,没有提到SQL注入或任何其他形式的操作。。。然而,我为我的工作项目所做的工作只是内联网,但我希望覆盖所有的基础,这是一个好习惯:)@JamieB我认为这表明youtube不是一个推荐的培训频道……我将在几天前给出它,只是试着通过这样做来学习所有这些,猜测我会犯所有的“经典错误”。你知道去哪里读这类东西吗?愿意在2-3个小时内不提问,哈哈。
IDbCommand command = conn.CreateCommand();
command.CommandText = "SELECT * FROM M_ITEM_SERIAL WHERE ITSR_PLANT_ID = @plantId";
IDbDataParameter plantIdParam = command.CreateParameter();
plantIdParam.ParameterName = "@plantId";
plantIdParam.Direction = ParameterDirection.Input;
plantIdParam.Value = plantId;
command.Parameters.Add(plantIdParam);