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. Interacting With a Database

Interacting With a Database

Scheduled Pinned Locked Moved C#
csharpdatabasequestion
7 Posts 3 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
    max29297
    wrote on last edited by
    #1

    I trying to interact with a Microsoft Access Database in an application. Interactions include creating, deleting, and editing entries. In Visual C# Studio, I have created a connection to the database, but I can't figure out what to do from there. Is there a way to cycle through the rows and columns in a database table using a for or foreach loop? I'm thinking it has something to do with the DataSet and other associated classes.


    If I had a sig, it would probably go here.


    P M 2 Replies Last reply
    0
    • M max29297

      I trying to interact with a Microsoft Access Database in an application. Interactions include creating, deleting, and editing entries. In Visual C# Studio, I have created a connection to the database, but I can't figure out what to do from there. Is there a way to cycle through the rows and columns in a database table using a for or foreach loop? I'm thinking it has something to do with the DataSet and other associated classes.


      If I had a sig, it would probably go here.


      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      Well, you can extract rows into a dataset and then use a foreach on the DataRows. For instance:

      foreach (DataRow dr in dsDataset.Tables[0])
      {
        // Do something with each row.
      }
      

      Alternatively, you can pull the data out with a DataReader and loop through them using:

      while (dr.MoveNext())
      {
        // Do something here.
      }
      

      Note that this is sometimes called a firehose cursor (something you may want to google).

      Deja View - the feeling that you've seen this post before.

      M 1 Reply Last reply
      0
      • P Pete OHanlon

        Well, you can extract rows into a dataset and then use a foreach on the DataRows. For instance:

        foreach (DataRow dr in dsDataset.Tables[0])
        {
          // Do something with each row.
        }
        

        Alternatively, you can pull the data out with a DataReader and loop through them using:

        while (dr.MoveNext())
        {
          // Do something here.
        }
        

        Note that this is sometimes called a firehose cursor (something you may want to google).

        Deja View - the feeling that you've seen this post before.

        M Offline
        M Offline
        max29297
        wrote on last edited by
        #3

        That's what I was thinking, but how do I extract rows into a dataset?


        If I had a sig, it would probably go here.


        P 1 Reply Last reply
        0
        • M max29297

          That's what I was thinking, but how do I extract rows into a dataset?


          If I had a sig, it would probably go here.


          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          Well, one way to do this is:

          DataSet ds ;
          using (OleDbConnection conn = new OleDbConnection("myConnectionDetails"))
          {
            using (OleDbDataAdapter cmd = new OleDbDataAdapter ("myCommand", conn))
            {
              conn.Open();
              try
              {
                cmd.Fill(ds);
                cmd.Close();
              }
              finally
              {
                if (conn.State == ConnectionState.Open)
                  conn.Close();
              }
            }
          }
          

          Deja View - the feeling that you've seen this post before.

          M 1 Reply Last reply
          0
          • P Pete OHanlon

            Well, one way to do this is:

            DataSet ds ;
            using (OleDbConnection conn = new OleDbConnection("myConnectionDetails"))
            {
              using (OleDbDataAdapter cmd = new OleDbDataAdapter ("myCommand", conn))
              {
                conn.Open();
                try
                {
                  cmd.Fill(ds);
                  cmd.Close();
                }
                finally
                {
                  if (conn.State == ConnectionState.Open)
                    conn.Close();
                }
              }
            }
            

            Deja View - the feeling that you've seen this post before.

            M Offline
            M Offline
            max29297
            wrote on last edited by
            #5

            Thanks, I'll try that!


            Hippophobia- Fear of horses. What... What?


            1 Reply Last reply
            0
            • M max29297

              I trying to interact with a Microsoft Access Database in an application. Interactions include creating, deleting, and editing entries. In Visual C# Studio, I have created a connection to the database, but I can't figure out what to do from there. Is there a way to cycle through the rows and columns in a database table using a for or foreach loop? I'm thinking it has something to do with the DataSet and other associated classes.


              If I had a sig, it would probably go here.


              M Offline
              M Offline
              max29297
              wrote on last edited by
              #6

              And is there a way to put the data from the DataSet back into the database?


              Hippophobia- Fear of horses. What... What?


              C 1 Reply Last reply
              0
              • M max29297

                And is there a way to put the data from the DataSet back into the database?


                Hippophobia- Fear of horses. What... What?


                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #7

                I recommend buying a book on ADO.NET, and perhaps one on SQL ( from the sounds of it )

                Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                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