Results 1 to 2 of 2

Thread: OleDbCommand

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    england
    Posts
    598

    OleDbCommand

    Hi,

    I have this code where I add 20 parameters to the OleDbCommand but for some reason I only get 1 record inserted, why is this?

    VB Code:
    1. OleDbCommand dbcommand = new OleDbCommand(sql2, conn2);
    2.  
    3.                             int Tablecount = 0;
    4.                             foreach (DataSet ds in TempDataSet)
    5.                             {
    6.                                 //foreach (string s in TempColumnName)
    7.                                 //{
    8.                                 string s = "@" + TempColumnName[Tablecount];
    9.                                 foreach (DataRow dr in ds.Tables[0].Rows)
    10.                                 {
    11.                                     //dbcommand.Parameters.Add(s, ds.Tables[0].Rows[ds.Tables[0].Rows.IndexOf(dr)][0]);
    12.                                     dbcommand.Parameters.AddWithValue(s, ds.Tables[0].Rows[ds.Tables[0].Rows.IndexOf(dr)][0]);
    13.                                 }
    14.                                 //}
    15.                                 Tablecount++;
    16.                             }
    17.  
    18.                             dbcommand.ExecuteNonQuery();

    Thanks Loftty,

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: OleDbCommand

    Firstly, parameters correspond to columns, not rows, so the number of parameters has no connection whatsoever to the number of records. You have one parameter and save a million records and vice versa, or any combination in between.

    Secondly, what is this supposed to achieve:
    Code:
    ds.Tables[0].Rows[ds.Tables[0].Rows.IndexOf(dr)][0]
    that wouldn't be achieved by this:
    Code:
    dr[0]
    Finally, you have misunderstood how to use parameters with a Datatable. You're only calling ExecuteNonQuery once so you're only going to affect one row. The idea is that you add parameters that know the column in your DataTable from which to draw the data. You then call the Update method of a DataAdapter and it handles getting the data from each individual row.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width