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. OleDbDataAdapter

OleDbDataAdapter

Scheduled Pinned Locked Moved C#
databasesql-serversysadmintutorialquestion
4 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.
  • M Offline
    M Offline
    Mehdi Mousavi
    wrote on last edited by
    #1

    Hi folks, Consider an SQL Server table that has got a PK (auto-identity column), which has been referenced by another table as a FK. Then imagine 2 DataTables that has been created to refer to the above-mentioned SQL Server tables, either partially or completely. I need to know how can I read/write these DataTables from/to those DataSources using the OleDbDataAdapter. It goes without saying that when you insert a row within the first source table, you'll get an IDENTITY that has to be referenced by the second table as the FK's value. The thing that I cannot understand is that how I can possibly interact with these 2 DataTable(s) through the OleDbDataAdapter and how to map DataSource-DataTable columns to each other as well as how to get the newly added IDENTITY and insert it in the second DataTable. Any sample would be highly appreciated Thanks in advance, Mehdi Mousavi - Software Architect [ http://mehdi.biz ]

    H 1 Reply Last reply
    0
    • M Mehdi Mousavi

      Hi folks, Consider an SQL Server table that has got a PK (auto-identity column), which has been referenced by another table as a FK. Then imagine 2 DataTables that has been created to refer to the above-mentioned SQL Server tables, either partially or completely. I need to know how can I read/write these DataTables from/to those DataSources using the OleDbDataAdapter. It goes without saying that when you insert a row within the first source table, you'll get an IDENTITY that has to be referenced by the second table as the FK's value. The thing that I cannot understand is that how I can possibly interact with these 2 DataTable(s) through the OleDbDataAdapter and how to map DataSource-DataTable columns to each other as well as how to get the newly added IDENTITY and insert it in the second DataTable. Any sample would be highly appreciated Thanks in advance, Mehdi Mousavi - Software Architect [ http://mehdi.biz ]

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

      First, don't use the OleDbDataAdapter for SQL Server! Use the SQL Server-specific classes in the System.Data.SqlClient namespace. They exhibit much more functionality and provide features that the generic OLE DB providers can't provide (at least in an abstract manner). Second, use a typed DataSet, or construct one programmatically at runtime (not fun). You can create an identity column in one of your tables. When you insert a row into that DataTable, the column value is incremented to the next identity using your incremental step. This allows your FK column to reference the identity PK. This is easy to do. The trick in a multi-user database-driven application is when you use that DataSet to update the database using SqlDataAdapter.Update. You should read the article, Inserting relational data using DataSet and DataAdapter[^]. Basically, your InsertCommand should also include a SELECT statement to make sure the DataSet is updated appropriately. The relationship that would exist in the DataSet for the PK/FK would update the FK with the PK value that was actually used when the row was inserted.

      Microsoft MVP, Visual C# My Articles

      M 1 Reply Last reply
      0
      • H Heath Stewart

        First, don't use the OleDbDataAdapter for SQL Server! Use the SQL Server-specific classes in the System.Data.SqlClient namespace. They exhibit much more functionality and provide features that the generic OLE DB providers can't provide (at least in an abstract manner). Second, use a typed DataSet, or construct one programmatically at runtime (not fun). You can create an identity column in one of your tables. When you insert a row into that DataTable, the column value is incremented to the next identity using your incremental step. This allows your FK column to reference the identity PK. This is easy to do. The trick in a multi-user database-driven application is when you use that DataSet to update the database using SqlDataAdapter.Update. You should read the article, Inserting relational data using DataSet and DataAdapter[^]. Basically, your InsertCommand should also include a SELECT statement to make sure the DataSet is updated appropriately. The relationship that would exist in the DataSet for the PK/FK would update the FK with the PK value that was actually used when the row was inserted.

        Microsoft MVP, Visual C# My Articles

        M Offline
        M Offline
        Mehdi Mousavi
        wrote on last edited by
        #3

        Heath Stewart wrote:First, don't use the OleDbDataAdapter for SQL Server! We might change the Database engine later from SQL Server 2000 to Oracle 9i, and that's why we tried to use the OleDB Data Provider. Heath Stewart wrote: Second, use a typed DataSet, or construct one programmatically at runtime (not fun). I don't see anything fun in this quote. I've never ever used an untyped DataSet, since it has got many drawbacks... Heath Stewart wrote: You should read the article, Inserting relational data using DataSet and DataAdapter[^]. Thanks a lot, I read the article and it will certainly solve my problem after two damn weeks of trying to post the same question here and there. I do really appreciate your help. Thank you for your time, Mehdi Mousavi - Software Architect [ http://mehdi.biz ]

        H 1 Reply Last reply
        0
        • M Mehdi Mousavi

          Heath Stewart wrote:First, don't use the OleDbDataAdapter for SQL Server! We might change the Database engine later from SQL Server 2000 to Oracle 9i, and that's why we tried to use the OleDB Data Provider. Heath Stewart wrote: Second, use a typed DataSet, or construct one programmatically at runtime (not fun). I don't see anything fun in this quote. I've never ever used an untyped DataSet, since it has got many drawbacks... Heath Stewart wrote: You should read the article, Inserting relational data using DataSet and DataAdapter[^]. Thanks a lot, I read the article and it will certainly solve my problem after two damn weeks of trying to post the same question here and there. I do really appreciate your help. Thank you for your time, Mehdi Mousavi - Software Architect [ http://mehdi.biz ]

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

          Mehdi Mousavi wrote: We might change the Database engine later from SQL Server 2000 to Oracle 9i, and that's why we tried to use the OleDB Data Provider. Reasonable enough. I just get used to saying this because so many with no such plans use the OLE DB provider and are trying to do SQL Server-specific things many times. Mehdi Mousavi wrote: I don't see anything fun in this quote. I've never ever used an untyped DataSet, since it has got many drawbacks... Constructing typed DataSet classes using the DataSet designer is definitely a heck of a lot easier than trying to code the same thing, plus gives you typed access to columns which is much faster. Sure, the same thing could be done manually, too, but again it can be tedious.

          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