SQL Insert query is executed twice

2019-08-02 16:13发布

问题:

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

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.

This is my 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?

UPDATE

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.

回答1:

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.

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.

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.



回答2:

I found that I had both a datatable and a dataset but only needed the datatable...

because I had them both running beside eachother in the same command, a duplicate was created...

make sure you use everything in the command and you understand what each one does even if your up awake for 2 days just like I was...

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

as you can see i had two sources filling one 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);

hope it helps someone...



回答3:

Do you really need a DataAdapter for this? Maybe you can try this.

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