Results 1 to 12 of 12

Thread: Excel OleDb

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Excel OleDb

    in C# (.NET 2.0) if we use a dataAdapter to fill an excel sheet into a dataset, how can we then update it, once changes have been made in the dataset?

    I cant use the CommandBuilder().GetUpdateCommand() as it complains it needs a primary key to do the update, which is true, but there is a primary key in the excel file, so how can I tell it to use that as the primary key?
    Last edited by Techno; Sep 13th, 2006 at 07:34 AM.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Excel OleDb

    You could use ExecuteNonQuery()

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

    Re: Excel OleDb

    You don't have to ever use a CommandBuilder and I recommend that you don't. Just write the non-query SQL statements for the DataAdapter yourself. If you wrote the SELECT statement you can just as easily write the others.
    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

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Excel OleDb

    well not quite.
    I guess OleDbCommandBuilder is handy for "newbies" - I was just trying to demonstrate something.
    Thing is, how can I then create the Update command? I obviously need to give it values for any rows which have been modified in the DataSet - how would I achieve this correctly?

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

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

    Re: Excel OleDb

    You add a parameter for each column you're updating. That's the only way to update using a DataAdapter and its exactly what a CommandBuilder does behind the scenes. Each parameter is mapped to a column in the DataTable using its SourceColumn property.
    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

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Excel OleDb

    I agree. Yes shouldve remembered that, this is what happens with 2 hours sleep!
    So if you can bare with me....how would I go about doing this again? What do I do?

    I create the oledbparameters (one parameter per column) and set the sourcecolumn
    add the parameters into the UpdateCommand parameter collection
    then what? What would the CommandText be?
    Last edited by Techno; Sep 13th, 2006 at 08:32 AM.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

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

    Re: Excel OleDb

    E.g.
    VB Code:
    1. myDataAdapter.UpdateCommand = New OleDbCommand("UPDATE MyTable SET Column1 = ?, Column2 = ? WHERE ID = ?", myConnection)
    2. myDataAdapter.UpdateCommand.Parameters.Add("Column1", OleDbType.VarChar, 50, "Column1")
    3. myDataAdapter.UpdateCommand.Parameters.Add("Column2", OleDbType.Integer, 0, "Column2")
    4. myDataAdapter.UpdateCommand.Parameters.Add("ID", OleDbType.Integer, 50, "ID")
    5.  
    6. myDataAdapter.UpdateCommand(myDataTable)
    Note that the first argument of the Add method is the name of the parameter. It is convention to use column name but it could be anything. The last parameter is the column name and must match the name of the DataColumn in the DataTable from which the data will be retrieved for that parameter. If you use the '?' place-holders your parameters must be added in the same order as they are used in the SQL code. Alternatively you can use named parameters, in which case the order doesn't matter:
    VB Code:
    1. myDataAdapter.UpdateCommand = New OleDbCommand("UPDATE MyTable SET Column1 = @Column1, Column2 = @Column2 WHERE ID = @ID", myConnection)
    2. myDataAdapter.UpdateCommand.Parameters.Add("@Column1", OleDbType.VarChar, 50, "Column1")
    3. myDataAdapter.UpdateCommand.Parameters.Add("@Column2", OleDbType.Integer, 0, "Column2")
    4. myDataAdapter.UpdateCommand.Parameters.Add("@ID", OleDbType.Integer, 50, "ID")
    5.  
    6. myDataAdapter.UpdateCommand(myDataTable)
    Note now that the first parameter to each call to the Add method matches the name of the parameter used in the SQL code. The Jet OLEDB provider accepts either format, but that is not the case for all providers.
    Last edited by jmcilhinney; Sep 13th, 2006 at 08:49 AM.
    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

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Excel OleDb

    yup - sorry, after i posted this I went out and just walking to the bus stop i remembered how to do this, once again many thanks and i will post back if i have any more queries

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  9. #9
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Excel OleDb

    Did you come running back from the bus stop?

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Excel OleDb

    no no lol

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

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

    Re: Excel OleDb

    Own up. You weren't walking to the bus stop. You were in "the smallest room in the house" weren't you? That's where all great thinking is done.
    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

  12. #12

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Excel OleDb

    LOL
    Thanks - everything seems to work great ;-)

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

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