Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 可以从数据库mysql C ASP.NET中选择,但不能插入数据库mysql C ASP.NET_C#_Mysql_Asp.net_Sql Insert - Fatal编程技术网

C# 可以从数据库mysql C ASP.NET中选择,但不能插入数据库mysql C ASP.NET

C# 可以从数据库mysql C ASP.NET中选择,但不能插入数据库mysql C ASP.NET,c#,mysql,asp.net,sql-insert,C#,Mysql,Asp.net,Sql Insert,我已经连接到我的数据库,我可以检索数据没有问题,但我无法插入。警报也没有显示,所以我确信查询没有被执行,它只是重定向到aspx页面。我尝试过许多不同的解决方案,但我认为问题不在于我在找什么。这是代码,希望有人能帮上忙 代码隐藏: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebContro

我已经连接到我的数据库,我可以检索数据没有问题,但我无法插入。警报也没有显示,所以我确信查询没有被执行,它只是重定向到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=\&quot;Web\&quot; /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,而不是针对MS SQL Server的库。此外,数据适配器往往更多地用于数据表和类似的东西;对于您的需要,只需执行命令对象就足够了

每个人都提到的安全漏洞是,您的查询最多只能破坏第二个fname、sname等。。。包含一个或多个撇号;您应该研究参数化查询以避免此类问题

编辑:另外,[和]是Microsoft数据库MS SQL Server和MS Access的字段分隔符;~键上的`由MySQL使用

Edit2:漏洞示例:

在[Staff]中插入职员姓名、职员姓名、职员电子邮件、职员密码值“+txtFName+”、“+txtSName+”、“+TXTMail+”、“+TXTMail+”、“+TXTSPassword+”


用户输入他们的名字为O’、、‘然后’、‘他们’、‘可以’、‘添加’、‘多个’、‘用户’、‘或者’、‘可能’、‘更差’、‘没有’、‘偶数’、‘导致’、‘安’、‘错误

MySQL或SQL Server?我敢肯定你不能在MySQL数据库中使用MSSQL api。不相关的提示:整个finally部分是多余的,并且由于使用块的原因可以删除。addStaffConn.Close;因为同样的原因是多余的。SqlCommand和SqlDataAdapter是一次性的,因此应该使用块。catch块正在报告它已成功。尽管AddWithValue被注释了,我还是建议阅读。我没有提到Sql注入漏洞,因为我猜你已经知道了,但我想了一下,然后又回来提到了它。@Richardismo我对Sql安全性不太了解,但我能发现的唯一漏洞是我将密码设置为password。这就是你说的吗?@uuerdo这是mysqls谢谢你指出这些,我试过不使用字段分隔符,我以前也用过@fname等,但我已经把它注释掉了。我真的只是试了很多东西。我使用的是MySQL.NET connector 6.9.11System.Data.SqlClient不是MySQL的.NET connector。您的配置中似乎同时包含MySQL和ms sql的连接字符串,并且没有使用MySQL连接字符串;如果您需要数据库可移植性System.Data.Odbc可能更合适。谢谢,这非常有用。我显然是个笨蛋,我感谢你的帮助。代码还有一些问题我正在解决,但很明显主要的问题是使用了错误的连接器。