【发布时间】:2016-03-25 13:49:51
【问题描述】:
我正在尝试将数据(用户输入)插入 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>
谁能确定这里的问题是什么?干杯。
【问题讨论】:
-
你得到的错误是?
-
您是否使用调试器单步执行代码..?你有任何错误
-
这段代码很糟糕。请不要这样写。它容易受到 SQL 注入的攻击。用参数化查询重写。
-
如果您没有看到错误文本,可能 Page.IsValid 为假。尝试单步执行代码并确保到达 try 块。
-
除了教科书上的 sql 注入漏洞示例之外,您还以纯文本形式存储密码。这也是一个很大的禁忌。它们应该加盐和散列。
标签: c# asp.net sql-server database