不将新记录保存到 SQL Server 数据库中

本文关键字:Server 数据库 SQL 新记录 保存 | 更新日期: 2023-09-27 18:31:44

问题是我的代码没有使用 asp.net c# 将新记录插入/保存到我的 SQL Server 数据库中,并且没有给我任何错误。

这是我的代码:

 public partial class AddNews_AddNews : System.Web.UI.Page
 {
        protected SqlConnection _connection;
        protected SqlCommand _command;
        protected SqlDataAdapter _adp;
        protected System.Data.DataTable _tbl;
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void Button1_Click1(object sender, EventArgs e)
        {
            prepareConnection();
            _command.CommandText = "INSERT INTO" + drbdlSection.SelectedItem + "(Title,Contect) VALUES (" + titleTextBox.Text + "," + CKEditor1.Text + ");";
        }
        protected void prepareConnection()
        {
            _connection = new SqlConnection(@"Data Source=localhost;Initial Catalog=BrainStorms;User ID=sa;Password=xxx");
            _connection.Open();
            _command = new SqlCommand();
            _command.Connection = _connection;
        }
    }

不将新记录保存到 SQL Server 数据库中

您需要

_command.ExecuteNonQuery();添加到 Button1_Click1() 方法的末尾。 您已经设置了要运行的查询,但从未实际运行过它。

应该执行命令。您的代码缺少对 ExecuteNonQuery 的调用

    protected void Button1_Click1(object sender, EventArgs e)
    {
        ....
        command.ExecuteNonQuery();
    }

话虽如此,我真的建议删除使命令文本的字符串连接。
您的代码对 SQL 注入和解析问题开放

所以我会以这种方式重写你的代码

    protected void Button1_Click1(object sender, EventArgs e)
    {
        string commandText = "INSERT INTO " + drbdlSection.SelectedItem.ToString() + 
                             "(Title,Contect) VALUES (@title, @edit)"
        using(SqlConnection con = prepareConnection())
        using(SqlCommand command = new SqlCommand(commandText, con))
        {
            command.Parameters.AddWithValue("@title", titleTextBox.Text);
            command.Parameters.AddWithValue("@edit", CKEditor1.Text);
            command.ExecuteNonQuery();
        } 
    }
    protected SqlConnection prepareConnection()
    {
        SqlConnection con = new SqlConnection(......);
        con.Open();
        return con;
    }

在此重写中,我更改了prepareConnection方法以返回 SqlConnection 的实例,并删除了创建命令的代码。
这将允许删除用于连接和命令的全局变量。

然后,在按钮事件中,我在连接周围添加了 using 语句,并在异常情况下也有助于关闭和销毁这些实例的命令。

最后,参数被添加到命令参数集合中,让任务将您的值传递给比你我更了解如何正确执行此操作的框架。

表名的串联仍然存在问题,但我希望您在之前准备好下拉列表的内容后可以完全控制此输入。

你不执行命令,你不打开连接,你的代码对 SqlInjection 使用参数开放!

public partial class AddNews_AddNews : System.Web.UI.Page
    {    
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void Button1_Click1(object sender, EventArgs e)
        {
            using(var connection = this.GetConnection())
            {
               using(var cmd = new SqlCommand())
               {
                  cmd.CommandText = "INSERT INTO " + drbdlSection.SelectedItem + "(Title, Contect) VALUES (@param1, @param2)";
                  cmd.Parameters.AddWithValue("@param1", titleTextBox.Text);
                  cmd.Parameters.AddWithValue("@param2", CKEditor1.Text);
                  cmd.Connection.Open();
                  cmd.ExecuteNonQuery();
               }
            }
        }
        protected SqlConnection GetConnection()
        {
            var connection = new SqlConnection(@"Data Source=localhost;Initial Catalog=BrainStorms;User ID=sa;Password=xxx");
            return connection;
        }
    }

Insert 语句,您的字符串周围没有单引号(除非您实际在文本框中输入单引号)...不确定这是否会导致问题;我不记得了 - 但值得一试。

而且由于您使用的是 ADO.NET,我认为您必须在设置我正在想的 ComamndText 后执行 _command 对象......