这是我第一次使用sql和asp.net。我正在研究一些例子,以确保我拥有所需的所有基础知识。我正在走一个教程,一切都应该正常工作,我得到一个.ExecuteNonQuery()错误。用户代码未对SqlException进行处理//关键字“Table”附近的语法不正确。
如果您有任何指示,请告诉我。我做了两次教程,我确定我在这里做错了。 -Thanks
.CS代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
namespace WebSite
{
public partial class _default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
con.Open();
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("insert into Table values('" + txtfName.Text + "','" + txtlName.Text + "','" + txtpNumber.Text + "')", con);
cmd.ExecuteNonQuery();
con.Close();
Label1.Visible = true;
Label1.Text = "Your DATA has been submitted";
txtpNumber.Text = "";
txtlName.Text = "";
txtfName.Text = "";
}
}
}
.aspx文件:
<form id="form1" runat="server">
<div class="auto-style1">
<strong>Insert data into Database<br />
<br />
</strong>
</div>
<table align="center" class="auto-style2">
<tr>
<td class="auto-style3">First Name:</td>
<td class="auto-style4">
<asp:TextBox ID="txtfName" runat="server" Width="250px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style3">Last Name:</td>
<td class="auto-style4">
<asp:TextBox ID="txtlName" runat="server" Width="250px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style3">Phone Number:</td>
<td class="auto-style4">
<asp:TextBox ID="txtpNumber" runat="server" Width="250px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style3"> </td>
<td class="auto-style4">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" Width="150px" />
</td>
</tr>
</table>
<br />
<br />
<asp:Label ID="Label1" runat="server" ForeColor="#663300" style="text-align: center" Visible="False"></asp:Label>
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [Table]"></asp:SqlDataSource>
</form>
SQL数据库:
CREATE TABLE [dbo].[Table] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[fName] VARCHAR (50) NOT NULL,
[lName] VARCHAR (50) NOT NULL,
[pNumber] VARCHAR (50) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
答案 0 :(得分:4)
通常,此错误消息是由输入文本框中的单引号或使用保留关键字引起的。您的查询中存在这两个问题。 TABLE字是reserved keyword for SQL Server,因此你应该用方括号封装它,而对于输入文本中可能存在单引号,正确的方法是使用Parameterized Query这样的
SqlCommand cmd = new SqlCommand("insert into [Table] values(@fnam, @lnam, @pNum)", con);
cmd.Parameters.AddWithValue("@fnam", txtfName.Text );
cmd.Parameters.AddWithValue("@lnam", txtlName.Text );
cmd.Parameters.AddWithValue("@pNum", txtpNumber.Text);
cmd.ExecuteNonQuery();
使用这种方法,您可以将工作转移到将输入文本解析为框架代码,从而避免解析文本和Sql Injection
的问题另外,我建议 NOT USE 一个全局变量来保留SqlConnection引用。这是一种昂贵的资源,如果您忘记关闭并处理它,您可能会对应用程序的性能和稳定性产生重大影响。
对于这种情况,using statement就是你真正需要的
using(SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings
["ConnectionString"].ConnectionString));
{
con.Open();
SqlCommand cmd = new SqlCommand("insert into [Table] values(@fnam, @lnam, @pNum)", con);
cmd.Parameters.AddWithValue("@fnam", txtfName.Text );
cmd.Parameters.AddWithValue("@lnam", txtlName.Text );
cmd.Parameters.AddWithValue("@pNum", txtpNumber.Text);
cmd.ExecuteNonQuery();
}
当然,删除全局变量和Page_Load中的open
答案 1 :(得分:3)
您的查询正在尝试插入名为Table的表中。那真的存在吗?如果没有,则将实际表名放入查询中。如果你的表真的叫做Table,那么我强烈建议你把它改成不那么混乱的东西。
此外,通过连接文本 now 停止编写命令。了解如何使用parameters来阻止SQL injection
修改强>
insert语句使用BOL documents for INSERT和examples provided therein中指定的格式。表是关键字,因此不要将其用作表名。如果必须使用关键字,则需要使用方括号将其转义。见BOL: Delimited Identifiers
我仍然说,不要使用“表”作为表的名称。让你的生活更轻松。
哦,编写安全代码(请参阅上面的注释,重新注入SQL,以及Linked In got hit的方式,以及花费多少)
答案 2 :(得分:0)
将'insert into Table values'更改为'insert into [Table] values',一切正常。感谢自己注意,远离简单的名字。
答案 3 :(得分:-1)
无论您使用ExecuteNonQuery(),都应该捕获SqlException,或者需要从函数中抛出。
在上面给出的情况下,Button1_Click
是使用SqlCommand类中的ExecuteNonQuery()的函数。
现在,该函数( ExecuteNonQuery )具有抛出SqlException的定义会发生什么。所以你有两个选择 - 你也可以抛出SqlException - 或者您可以将此行放在try catch块中以处理异常。