这是我第一次使用sql和asp.net。我正在研究一些例子,以确保我拥有所需的所有基础知识。我正在走一个教程,一切都应该正常工作,我得到一个.ExecuteNonQuery()错误。用户代码未对SqlException进行处理//关键字“Table”附近的语法不正确。
如果您有任何指示,请告诉我。我做了两次教程,我确定我在这里做错了。 -谢谢
.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)
);
通常,此错误消息是由输入文本框中的单引号或使用保留关键字引起的。您的查询中存在这两个问题。 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时出现问题
另外,我建议不要使用全局变量来保留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
您的查询正在尝试插入名为Table的表中。那真的存在吗?如果没有,则将实际表名放入查询中。如果你的表真的叫做Table,那么我强烈建议你把它改成不那么混乱的东西。
此外,现在通过连接文本停止编写命令。了解如何使用parameters以防止SQL injection
编辑
insert语句使用BOL documents for INSERT和examples provided therein中指定的格式。表是关键字,因此不要将其用作表名。如果必须使用关键字,则需要使用方括号将其转义。见BOL: Delimited Identifiers
我仍然说,不要使用“表”作为表的名称。让您的生活更轻松。
哦,并编写安全代码(请参阅上面的注释重新SQL注入,以及如何Linked In got hit,以及它们花了多少钱)
将'insert into Table values'更改为'insert into [Table] values',一切正常。谢谢注意自己,远离简单的名字。
无论您使用ExecuteNonQuery(),您都应该捕获SqlException,或者需要从函数中抛出。
在上面给出的情况下,Button1_Click
是使用来自SqlCommand类的ExecuteNonQuery()的函数。
现在,该函数(ExecuteNonQuery)具有抛出SqlException的定义会发生什么。所以你有两个选项 - 你也可以抛出SqlException - 或者你可以把这行放在try catch块来处理Exception。