SQL插入查询执行两次 [英] SQL Insert query is executed twice

查看:220
本文介绍了SQL插入查询执行两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用<一个href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter.InsertCommand.aspx"相对=nofollow> adapter.InsertCommand 插入一些数据插入表中。

I'm using adapter.InsertCommand to insert some data into a table.

唯一的问题是,它的执行两次,从而使我在DB的双项。 我试过下面 adapter.InsertCommand 的文档和我自己的code的例子,但得到同样的结果。

The only problem is that it's executed twice, thus giving me double entries in the DB. I've tried following the example in the documentation of adapter.InsertCommand and my own code, but get the same result.

这是我的code:

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();

    }
}

在我怎样才能避免这种情况安妮的建议?

Anny suggestions in how I can avoid this?

更新

右键,一些调试后,我发现我的 newWallComplaint_Click 函数被解雇了两次。这是becuae我有以下code:

Right, after some debugging, I discovered that my newWallComplaint_Click function was fired twice. This was becuae I had the following code:

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

不检查回传,该执行我的功能后,也提交。因此,为了避免我的函数运行两次,我添加了一个检查回发。

Not checking for PostBack, this executes my function after submit also. So to avoid having my function run twice, I added a check for PostBack.

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

    }

现在我的查询不运行两次。

Now my query is not run twice.

推荐答案

我什么都看不到你的code,将执行了两次。我认为它是被调用两次。把一个破发点,在 addNewComment ,如果它被称为两次看堆栈跟踪,看看它是从两个场合呼吁。

I can't see anything in your code that would execute it twice. I'd assume that it is being called twice. Put a break point at addNewComment and if it is being called twice look at the stack traces to see where it is being called from on both occasions.

也许你有一个事件被调用两次为例。这可以在ASP.NET发生,如果你俩都启用了事件的自动布线,并已连接事件了明确。

Maybe you have an event being called twice for example. This can happen in ASP.NET if you both have auto wiring of events enabled and have wired the event up explicitly.

顺便说一句,你绝对应该使用参数化查询的不是字符串连接。我假设注释用户提供的输入?在这种情况下,你是在和自己的SQL注入攻击和你已经表明了code。

By the way you should definitely use parametrized queries not string concatenation. I'm assuming that comment is user supplied input? In which case you are setting yourself up for a SQL injection attack with the code you have shown.

这篇关于SQL插入查询执行两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆