【发布时间】:2020-01-21 18:13:50
【问题描述】:
我正在使用 Visual Studio 2019 和 SQL Server Management Studio。
CustomerDetails.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CustomerDetails.aspx.cs" Inherits="CustomerInfo.CustomerDetails" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
text-align: center;
}
.auto-style2 {
width: 29%;
height: 119px;
margin-left: 51px;
}
.auto-style3 {
width: 240px;
}
.auto-style4 {
width: 240px;
text-align: right;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<h3 class="auto-style1">Enter Details</h3>
</div>
<p>
</p>
<table align="center" class="auto-style2">
<tr>
<td class="auto-style4">User Name :</td>
<td>
<asp:TextBox ID="name" runat="server" Width="200px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style4">Email :</td>
<td>
<asp:TextBox ID="mail" runat="server" Width="200px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style4">Password :</td>
<td>
<asp:TextBox ID="password" runat="server" Width="200px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style3"> </td>
<td>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" Width="100px" />
</td>
</tr>
</table>
<asp:Label ID="Label1" runat="server" ForeColor="Green" Visible="False"></asp:Label>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Customer_Info_TableConnectionString %>" SelectCommand="SELECT * FROM [cus_table]"></asp:SqlDataSource>
</form>
</body>
</html>
我得到了我如何创建网页设计的确切输出,它允许我在按下按钮后在字段中输入详细信息我收到此错误:
抛出异常:System.Data.dll 中的“System.Data.SqlClient.SqlException”
System.Data.dll 中出现“System.Data.SqlClient.SqlException”类型的异常,但未在用户代码中处理
')' 附近的语法不正确。
CustomerDetails.aspx.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 CustomerInfo
{
public partial class CustomerDetails : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Customer_Info_TableConnectionString"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("insert into [dbo].[cus_table]('" + name.Text+"','"+name.Text+"','"+mail.Text+"','"+password.Text+"')",con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Label1.Visible = true;
Label1.Text = "Information uploaded Successfully! :)";
name.Text = "";
mail.Text = "";
password.Text = "";
}
}
}
web.config
<connectionStrings>
<add name="Customer_Info_TableConnectionString"
connectionString="Data Source=DESKTOP-B44TBSQ;Initial Catalog=Customer_Info_Table;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
SQL 服务器:
CREATE TABLE [dbo].[cus_table]
(
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [nvarchar](50) NOT NULL,
[mail] [nvarchar](50) NOT NULL,
[password] [nvarchar](50) NOT NULL
) ON [PRIMARY]
GO
【问题讨论】:
-
永远,永远,永远,从用户提供的字符串构造一个 sql 语句。始终使用参数绑定。
-
建议:修复bug后,尝试在某处设置一个包含单引号的密码。
-
@Nisarg 啊,
fish'); delete from cus_table; --众所周知的密码(Raja 注意:不要输入此密码,因为密码不安全,它会破坏你的数据库)
标签: c# asp.net sql-server