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# 从asp.net将数据插入SQL Server数据库_C#_Asp.net_Sql Server_Database - Fatal编程技术网

C# 从asp.net将数据插入SQL Server数据库

C# 从asp.net将数据插入SQL Server数据库,c#,asp.net,sql-server,database,C#,Asp.net,Sql Server,Database,我正在尝试将数据(用户输入)插入SQL Server数据库。看起来一切正常,但实际上并没有向数据库中插入任何数据。代码隐藏文件(default.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.Web.Configur

我正在尝试将数据(用户输入)插入SQL Server数据库。看起来一切正常,但实际上并没有向数据库中插入任何数据。代码隐藏文件(default.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.Web.Configuration;
using System.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection connection = new SqlConnection(WebConfigurationManager.ConnectionStrings["mumsDiaryConnectionString"].ConnectionString);

    protected void reg_submit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            // storing user input data into variable
            var first = reg_first.Text;
            var last = reg_last.Text; 
            var email = reg_email.Text;
            var pass = reg_pass.Text.GetHashCode().ToString();
            var sub = reg_sub.Text;
            var state = reg_state.Text;
            var post = reg_post.Text;
            var country = "Australia";

            try  
            {
                 connection.Open();
                 SqlCommand cmd = new SqlCommand("INSERT INTO user(first, last, email, password, suburb, postcode, state, country) VALUES('"+first+"','"+last+"','"+email+"','"+pass+"','"+sub+"','"+post+"','"+state+"','"+country+"')", connection);
                 cmd.ExecuteNonQuery();
            }
            catch(Exception err)
            {
                Label10.Text = "something gone wrong";
                Label10.Text += err.Message;        
            }
            finally
            {
                connection.Close();
                //  Response.Redirect("~/Pages/Home_page.aspx");
            }
      }
 }
}  
这是我的web.config文件,看起来像:

<?xml version="1.0"?>
<!--For more information on how to configure your ASP.NET application, please visit http://go.microsoft.com/fwlink/?LinkId=169433-->

<configuration>
<connectionStrings>
   <add name="mumsDiaryConnectionString" connectionString="Data Source=MDASHIFURRA73C7\SQLEXPRESS;Initial Catalog=mumsDiary;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
  <compilation debug="true" targetFramework="4.6" />
  <httpRuntime targetFramework="4.6" />
</system.web>

<appSettings>
  <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
</appSettings>
</configuration>  


有人能确定这里的问题是什么吗?干杯

遗憾的是,我们没有足够的信息来正确诊断您的问题,从我看来,这些都是潜在的问题和安全缺陷。您的查询的结构应该更像:

private readonly string dbConnection = ConfigurationManager.ConnectionString[".."].ConnectionString;
private const string queryInsertUser = "INSERT INTO [User] ([FirstName]) VALUES (@FirstName);"

protected void SaveUser(string firstName)
{
     using(var connection = new SqlConnection(dbConnection))
     using(var command = new SqlCommand(queryInsertUser, connection))
     {
          connection.Open();
          command.Parameters.Add("@FirstName", SqlDbType.NVarChar).Value = first;
          command.ExecuteNonQuery();
     }
}
因此,上面所述将阻止您引入的Sql注入,您可以在web上获得关于用户密码的Salt/Hash的优秀文章。至于其他问题,您需要检查以下内容:

  • 运行跟踪时,您是否看到查询命中了Sql实例
  • 当您单步执行代码时,它是否会进入
    catch
  • Sql实例是否实际运行,是否允许应用程序的用户帐户运行

基于该准则,尽管它不是“标准实践”,但它应该是有效的。因此,我相信您的问题源于您的Sql Express
ConnectionString
或实例本身。如果没有更多信息,将很难正确诊断,但是如果您正确地命中了数据库,将显示跟踪。

而您得到的错误是?您是否使用了调试器来逐步执行代码。。?你有什么错误吗?这个密码不正确。请不要这样写。它容易受到SQL注入的攻击。使用参数化查询重写。如果没有看到错误文本,则Page.IsValid可能为false。尝试单步执行代码并确保达到Try块。除了教科书中的sql注入漏洞示例外,您还以纯文本形式存储密码。这也是一个很大的禁忌。它们应该被腌制和切碎。非常感谢你的朋友。我是编程新手。还有很多东西要学。不管怎样,单步执行代码非常有效。并且能够确定问题所在。谢谢你的帮助@格雷格