locked
Way to batch add records from a bindingSource RRS feed

  • Question

  • I have a form which is filled using a bindingSource from a DataSet. On submition of the form I then require X duplicate records to be created using the form data. Where X can be anything from 0 to several hundred.

    I tried doing this by using a loop and calling the command bindingSource.AddNew() then repopulating all the fields and saving it etc, but its very slow.

    So the only other way I can think of is to instead call the tableAdaptor.Insert command in a loop for the X records. The only issue is the cmd needs parameters passed to it, but how can I populate the parameters since the data in the binding source includes dates/numbers/null etc. Casting each one is a pain and also if I update the form requires each new field to be added in.

    So does anyone know a way I can solve this problem?
    Tuesday, August 5, 2008 1:54 PM

Answers

  •  

    Hi;

     

    If I understand you correctly you want to make copies of a record that is currently display on your form ?

     

    If this is the case then have you considered using an Array, or a similar data structure to hold copies of the data.

     

    Good Luck

     

     

    Tuesday, August 5, 2008 4:36 PM
  • Hi adamp

     

    As far as I know, if you just want to make it faster to batch add records you should share a SqlConnection and do all actions between the SqlConnection.Open() and SqlConnection.Close() instead of open the connection for many times.

    Here is two example:

    Code Snippet

    SqlConnection sqlConn = new SqlConnection("Your Conncetion String");

    sqlConn.Open();

    for (int i = 0; i < X; i++)

    {

        ……

    }

    sqlConn.Close();

     

     

    instead of

    Code Snippet

    for (int i = 0; i < X; i++)

    {

        SqlConnection sqlConn = new SqlConnection("Your Conncetion String");

    sqlConn.Open();

     

    ……

     

        sqlConn.Close();

    }

     

     

    Share the same connection will save database resource.

     

    Best Regards,

    Kira Qian

     

    Windows Forms General FAQs
    Windows Forms Data Controls and Databinding FAQs

     

    Friday, August 8, 2008 7:45 AM

All replies

  •  

    Hi;

     

    If I understand you correctly you want to make copies of a record that is currently display on your form ?

     

    If this is the case then have you considered using an Array, or a similar data structure to hold copies of the data.

     

    Good Luck

     

     

    Tuesday, August 5, 2008 4:36 PM
  • Hi adamp

     

    As far as I know, if you just want to make it faster to batch add records you should share a SqlConnection and do all actions between the SqlConnection.Open() and SqlConnection.Close() instead of open the connection for many times.

    Here is two example:

    Code Snippet

    SqlConnection sqlConn = new SqlConnection("Your Conncetion String");

    sqlConn.Open();

    for (int i = 0; i < X; i++)

    {

        ……

    }

    sqlConn.Close();

     

     

    instead of

    Code Snippet

    for (int i = 0; i < X; i++)

    {

        SqlConnection sqlConn = new SqlConnection("Your Conncetion String");

    sqlConn.Open();

     

    ……

     

        sqlConn.Close();

    }

     

     

    Share the same connection will save database resource.

     

    Best Regards,

    Kira Qian

     

    Windows Forms General FAQs
    Windows Forms Data Controls and Databinding FAQs

     

    Friday, August 8, 2008 7:45 AM