using System; using System.Data.OleDb; public void getColumnNames() { string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=MyDatabase.mdb;"; string query = "SELECT column1, column2, column3 FROM MyTable;"; OleDbConnection connection = new OleDbConnection(connectionString); OleDbCommand command = new OleDbCommand(query, connection); connection.Open(); OleDbDataReader reader = command.ExecuteReader(); for (int i = 0; i < reader.FieldCount; i++) { Console.WriteLine(reader.GetName(i)); } reader.Close(); connection.Close(); }
using System; using System.Data.OleDb; public void getColumnName() { string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=MyDatabase.mdb;"; string query = "SELECT column1 FROM MyTable WHERE column2 = ?;"; OleDbConnection connection = new OleDbConnection(connectionString); OleDbCommand command = new OleDbCommand(query, connection); command.Parameters.AddWithValue("@column2Value", "someValue"); connection.Open(); OleDbDataReader reader = command.ExecuteReader(); if (reader.Read()) { Console.WriteLine("Column Name: " + reader.GetName(0)); Console.WriteLine("Value: " + reader.GetValue(0)); } reader.Close(); connection.Close(); }This example retrieves the value of the first column in a SELECT query where the value of the second column matches a parameterized value. It then prints the column name and value to the console.