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. Returning all fields in a database

Returning all fields in a database

Scheduled Pinned Locked Moved C#
databasecsharpc++mysqlsysadmin
2 Posts 1 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.
  • G Offline
    G Offline
    gonad
    wrote on last edited by
    #1

    Hi everyone. (this is my first post to this board, so please forgive me if it doesn't look right). I am new to c# and i am wondering if someone can help me write a for loop to return all the fields and data in a database. This is the code and it works 100%, but i want it to query the table fields and data dynamically instead of what i have. using System; using Microsoft.Data.Odbc; using Console = System.Console; namespace practice { public class dbconnection { public void swdata() { string msg; string native; string src; string sql; try { string _db = "DRIVER={MySQL ODBC 3.51 Driver};" + "SERVER=;DATABASE=;" + "UID=;PASSWORD=;" + "OPTION=512"; OdbcConnection dbconnect = new OdbcConnection(_db); dbconnect.Open(); OdbcCommand my_cmd = new OdbcCommand("",dbconnect); my_cmd.CommandText = "SELECT * FROM swdata_o"; OdbcDataReader my_dr; my_dr = my_cmd.ExecuteReader(); // RETURN ALL FIELDS IN DATABASE - FOR LOOP while(my_dr.Read()) { Console.WriteLine(my_dr.GetInt32(0) + "," + my_dr.GetString(1) + "," + my_dr.GetString(2)); } dbconnect.Close(); } catch(OdbcException odbcCon) { for(int i=0;i Thanks for your help. :)

    G 1 Reply Last reply
    0
    • G gonad

      Hi everyone. (this is my first post to this board, so please forgive me if it doesn't look right). I am new to c# and i am wondering if someone can help me write a for loop to return all the fields and data in a database. This is the code and it works 100%, but i want it to query the table fields and data dynamically instead of what i have. using System; using Microsoft.Data.Odbc; using Console = System.Console; namespace practice { public class dbconnection { public void swdata() { string msg; string native; string src; string sql; try { string _db = "DRIVER={MySQL ODBC 3.51 Driver};" + "SERVER=;DATABASE=;" + "UID=;PASSWORD=;" + "OPTION=512"; OdbcConnection dbconnect = new OdbcConnection(_db); dbconnect.Open(); OdbcCommand my_cmd = new OdbcCommand("",dbconnect); my_cmd.CommandText = "SELECT * FROM swdata_o"; OdbcDataReader my_dr; my_dr = my_cmd.ExecuteReader(); // RETURN ALL FIELDS IN DATABASE - FOR LOOP while(my_dr.Read()) { Console.WriteLine(my_dr.GetInt32(0) + "," + my_dr.GetString(1) + "," + my_dr.GetString(2)); } dbconnect.Close(); } catch(OdbcException odbcCon) { for(int i=0;i Thanks for your help. :)

      G Offline
      G Offline
      gonad
      wrote on last edited by
      #2

      I found my answer... :) // RETURN ALL FIELDS IN DATABASE while(my_dr.Read()) { for(int i=0;i

      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