Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Updating table from datatable

Updating table from datatable

Scheduled Pinned Locked Moved C#
databasetutorialquestionannouncement
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    zuhx
    wrote on last edited by
    #1

    I am trying to update a table from a datatable. The datatable is populated first from a text file and then I would like to get that data into a database datatable. However the code below does not work, part of it is I do not know how to associate the connection string to the data adapter. Also, is there anything else I need to do? Do I need to have separate Update, Insert, and Delete commands or is it possible to just call the Update command? ConnectToDb connection = new ConnectToDb().conn; //this returns a SqlConnection SqlDataAdapter da = new SqlDataAdapter(); da.Update(ds.Tables["TradeOrderFillsPre"]);

    H 1 Reply Last reply
    0
    • Z zuhx

      I am trying to update a table from a datatable. The datatable is populated first from a text file and then I would like to get that data into a database datatable. However the code below does not work, part of it is I do not know how to associate the connection string to the data adapter. Also, is there anything else I need to do? Do I need to have separate Update, Insert, and Delete commands or is it possible to just call the Update command? ConnectToDb connection = new ConnectToDb().conn; //this returns a SqlConnection SqlDataAdapter da = new SqlDataAdapter(); da.Update(ds.Tables["TradeOrderFillsPre"]);

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      Reading the table into a DataTable and adding that to a DataSetis what I would recommend, but iterating over the list of values in the text file and using a simple SqlCommand with SqlParameters (so you declare the command one and parameterize it to simply change the values and call ExecuteNonQuery) would also be easy and more efficient. If you want to use the SqlDataAdapter, you must at least have the SelectCommand assigned to with a SqlCommand for your SELECT statement. In order to insert, update, or delete rows, however, you must have the InsertCommand, UpdateCommand, and DeleteCommand assigned on the SqlDataAdapter. You can use the SqlCommandBuilder with the SelectCommand if the command is simple enough (i.e., JOINs and other advanced statements are not supported). Also, the SelectCommand must be parameterized (see the SqlCommand.Parameters property). See the SqlDataAdapter class documentation in the .NET Framework SDK (installed by default with VS.NET, and available online from http://msdn.microsoft.com/netframework[^]) for more information and an example of how to use some of these classes.

      Microsoft MVP, Visual C# My Articles

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups