【问题标题】:SQL Insert query is executed twiceSQL Insert 查询执行两次
【发布时间】:2010-07-22 13:07:13
【问题描述】:

我正在使用adapter.InsertCommand 向表中插入一些数据。

唯一的问题是它执行了两次,因此在数据库中给了我双重条目。 我已经尝试按照adapter.InsertCommand 文档中的示例和我自己的代码进行操作,但得到了相同的结果。

这是我的代码:

public class nokernokDAL
{
    SqlConnection connection = new SqlConnection();
    SqlDataAdapter adapter = new SqlDataAdapter();

    public nokernokDAL()
    {
        connection.ConnectionString = EPiServer.Global.EPConfig["EPsConnection"].ToString();
        connection.Open();
    }

    public void addNewComment(int userID, int pageID, string title, string comment)
    {
        string query = "INSERT INTO dbo.nokernok_kommentarer (userID, pageID, commentTitle, comment) " +
                       "VALUES ("+ userID +", "+ pageID +", '"+ title +"', '"+ comment +"')";

        adapter.InsertCommand = new SqlCommand(query, connection);
        adapter.InsertCommand.ExecuteNonQuery();

    }
}

关于如何避免这种情况的任何建议?

更新

对,经过一些调试,我发现我的newWallComplaint_Click 函数被触发了两次。这是因为我有以下代码:

    protected void Page_Load(object sender, EventArgs e)
    {
            btnNewWallComplaint.Click += new EventHandler(this.newWallComplaint_Click);
    }

不检查 PostBack,这也会在提交后执行我的功能。所以为了避免我的函数运行两次,我添加了一个 PostBack 检查。

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            btnNewWallComplaint.Click += new EventHandler(this.newWallComplaint_Click);
        }

    }

现在我的查询没有运行两次。

【问题讨论】:

  • 进行一些调试并确定该函数是否以某种方式运行了两次,或者它实际上是否执行了两次 SQL。
  • 我猜你的标题说明了一切——“SQL 插入查询确实执行了两次”!
  • 我见过用户“双击”一个按钮(按钮只需单击一下),导致底层代码运行两次,你确定这里没有发生类似的事情吗?
  • 调试在 Visual Web Developer 2008 Express 中不起作用(对我来说)。所以我坚持输出带有数据的字符串。在得到你们确认我的 SQL 代码没有问题之后,我开始寻找其他地方。请参阅我上面的解决方案。谢谢大家!
  • 您不需要检查 IsPostBack 来连接事件。这是 Page_Init 中的每个请求都应该发生的事情。可能发生的情况是您启用了 AutoEventWireUp 并且您还明确地连接了一个点击事件。我敢打赌,如果您关闭 AutoEventWireUp 并将点击事件的连接移动到 Page_Init,它会起作用(并删除 IsPostBack 条件)。

标签: c# .net sql sql-server-2005


【解决方案1】:

我在您的代码中看不到任何会执行两次的内容。我假设它被调用了两次。在addNewComment 处放置一个断点,如果它被调用两次,请查看堆栈跟踪以查看两次调用它的位置。

例如,也许您有一个事件被调用了两次。如果您同时启用了事件的自动关联并明确关联了事件,则这可能在 ASP.NET 中发生。

顺便说一句,您绝对应该使用parametrized queries 而不是字符串连接。我假设评论是用户提供的输入?在这种情况下,您将使用您显示的代码为自己设置 SQL 注入攻击。

【讨论】:

  • 谢谢。调试不起作用,但我擅长输出字符串。结果我没有在页面加载时使用 if(!isPostBack) ,因此执行了我的脚本两次。查看更新的代码。
【解决方案2】:

我发现我既有数据表又有数据集,但只需要数据表...

因为我让它们在同一个命令中并排运行,所以创建了一个副本...

确保您使用了命令中的所有内容,并且您了解每个命令的作用,即使您像我一样醒了 2 天...

            DataSet dataset = new DataSet();

            data.Fill(dataset, "data");

            // Populate a new data table and bind it to the BindingSource.
            DataTable datatable = new DataTable();
            data.Fill(datatable);

如您所见,我有两个来源填充一个 MySqlDataAdapter...

            //removing the following two lines fixed my duplicates issue...
            //DataSet dataset = new DataSet();
            //data.Fill(dataset, "data");

            // Populate a new data table and bind it to the BindingSource.
            DataTable datatable = new DataTable();
            data.Fill(datatable);

希望对某人有所帮助...

【讨论】:

    【解决方案3】:

    你真的需要一个 DataAdapter 吗?也许你可以试试这个。

     public class nokernokDAL
     {
         string connectionString;
    
         public nokernokDAL()
         {
             ConnectionString = EPiServer.Global.EPConfig["EPsConnection"].ToString();
         }
    
         public void addNewComment(int userID, int pageID, string title, string comment)
         {
             string query = "INSERT INTO dbo.nokernok_kommentarer (userID, pageID, commentTitle, comment) " +
                            "VALUES ("+ userID +", "+ pageID +", '"+ title +"', '"+ comment +"')";
    
             using (SqlConnection conn = new SqlConnection(_connString))
             {
                 SqlCommand cmd = conn.CreateCommand();
                 cmd.CommandText = Query;
                 conn.Open();
                 cmd.ExecuteNonQuery();
             }
         }
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-18
      • 2021-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多