【发布时间】:2018-04-16 21:16:54
【问题描述】:
我与我的数据库建立了连接,我可以毫无问题地检索数据,但我无法插入。警报也没有显示,所以我确定查询没有被执行,它只是重定向到 aspx 页面。我尝试了许多不同的解决方案,但我认为问题不在于我正在寻找的地方。这是代码,我希望有人可以提供帮助。
后面的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Threading;
using System.Threading.Tasks;
namespace Childrens.Admin
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void btnViewStaff_ServerClick(object sender, EventArgs e)
{
divAddStaff.Visible = false;
staffGridView.Visible = true;
}
protected void btnAddNewStaff_ServerClick(object sender, EventArgs e)
{
staffGridView.Visible = false;
divAddStaff.Visible = true;
}
protected void btnSubmitStaff_ServerClick(object sender, EventArgs e)
{
if (txtPassword == txtCPassword)
{
using (SqlConnection addStaffConn = new SqlConnection(ConfigurationManager.ConnectionStrings["myConn"].ToString()))
{
try
{
addStaffConn.Open();
string query = "INSERT INTO [Staff] (staff_fname,staff_sname,staff_email,staff_pass) VALUES ('" + txtFName + "','" + txtSName + "','" + txtEmail + "','" + txtPassword+"')"; //(@fname,@sname,@email,@pass)";
SqlDataAdapter staffAdapter = new SqlDataAdapter();
SqlCommand addStaffCommand = new SqlCommand(query, addStaffConn);
/*addStaffCommand.Parameters.AddWithValue("@fname", txtFName);
addStaffCommand.Parameters.AddWithValue("@sname", txtSName);
addStaffCommand.Parameters.AddWithValue("@email", txtEmail);
addStaffCommand.Parameters.AddWithValue("@pass", txtPassword);*/
staffAdapter.InsertCommand = addStaffCommand;
staffAdapter.InsertCommand.ExecuteNonQuery();
addStaffConn.Close();
Response.Write(String.Format("<script>alert('The entry was successful!');window.location='{0}';</script>", "URL=staff.aspx"));
}
catch (Exception ex)
{
Response.Write(String.Format("<script>alert('The entry was successful!');window.location='{0}';</script>", "URL=staff.aspx"));
}
finally
{
if (addStaffConn.State == System.Data.ConnectionState.Open)
{
addStaffConn.Close();
}
addStaffConn.Dispose();
}
}
}
}
}
}
web.config 文件:
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please
visit
https://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.6.1"/>
<httpRuntime targetFramework="4.6.1"/>
</system.web>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.8.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701"/>
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider,
Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.8.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35"
warningLevel="4" compilerOptions="/langversion:default /nowarn:41008
/define:_MYTYPE=\"Web\" /optionInfer+"/>
</compilers>
</system.codedom>
<connectionStrings>
<add name="myConn" connectionString="server=localhost;user
id=root;persistsecurityinfo=True;database=childrens" />
<add name="childrensConnectionString"
connectionString="server=localhost;user id=root;password=password;persistsecurityinfo=True;database=childrens;allowuservariables=True"
providerName="MySql.Data.MySqlClient" />
</connectionStrings>
</configuration>
【问题讨论】:
-
MySQL 还是 SQL Server?我很确定您不能将 (MS)SQL api 用于 MySQL 数据库。
-
不相关提示:整个
finally部分是多余的,可以由于using块而被删除。出于同样的原因,addStaffConn.Close();是多余的。 SqlCommand 和 SqlDataAdapter 是一次性的,所以应该在using块中。 catch 块报告它是成功的。虽然 AddWithValue 已被评论,但我建议阅读can we stop using AddWithValue。 -
我没有提到Sql注入漏洞,因为我猜你已经知道了,但我想了想,又回来提了。
-
@Richardissimo 我对 sql 安全性不太了解,但我能发现的唯一漏洞是我将密码设置为密码。你说的是这个吗?
-
@Uueerdo 这是 MySQL
标签: c# mysql asp.net sql-insert