c# - System.Data.SqlClient.SqlException:'')' 附近的语法不正确。' 是那个sql查询错误还是什么?
问题描述
我正在使用 Visual Studio 2019 和 SQL Server Management Studio。
客户详细信息.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 = "";
}
}
}
网络配置
<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(参见:SQL 注入);使用参数,最好使用像 dapper 这样的工具
- 建议在插入时指定列顺序以防止歧义
- 永远不要将密码存储为文本;用盐对它们进行散列以进行身份验证(冗长但很重要的主题),或者如果您正在编写密码管理器,请使用强加密
为了方便起见,这个例子使用了Dapper;请注意,Dapper 还处理打开和关闭连接、参数打包以及(在使用Query<T>
等时)将结果解析为实例。ADO.NET 的所有无聊部分!
var pwHash = // ... not shown; something involving password.Text
// note: you'd usually need to invent and store the "salt", too
conn.Execute(@"
insert into [dbo].[cus_table] (name, mail, passwordHash)
values (@name, @mail, @pwHash)",
new {
name = name.Text,
mail = mail.Text,
pwHash = pwHash
});
Label1.Visible = true;
Label1.Text = "Information uploaded Successfully! :)";
注意:以上内容不会直接起作用,因为您有一个password
列而不是passwordHash
列。但重要的是:你的password
专栏只是一个不应该存在的东西。我不能强迫你修复它,但是:请做。
糟糕的安全版本将类似于:
conn.Execute(@"
insert into [dbo].[cus_table] (name, mail, password)
values (@name, @mail, @password)",
new {
name = name.Text,
mail = mail.Text,
password = password.Text // TODO: hash (for auth) or encrypt (pw manager)
});
推荐阅读
- r - 使用 purrr 从嵌套的 tibble 中编写具有多个 sheetNames 的 excel 表
- python - 使用 NETCDF4 中的 MODIS 数据从 python 中的数据框中选择特定日期
- javascript - 锚链接在粘性标题后面的页面顶部。如何设置平滑滚动 JS 在滚动时始终保持 75px 的上边距?
- java - 如何从android kotlin中的recyclerview适配器获取arraylist
- html - 如何在不循环使用关键帧动画的情况下更改内容
- html - 访问时,我的网站无法在 iPhone12 pro 的谷歌应用程序中正确加载抛出一个链接
- c++ - 具有多种数据类型的数组
- react-native - React Native TypeError:未定义不是对象(评估'options.routeGetIdList [action.payload.name]')
- java - 如何遍历多个文件夹并使用Java比较存储在其中的pdf
- tensorflow - 为什么单个 LSTM 输出不出现在 return_sequence 输出中?