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