Hello,
I have a GitHub code sample were you can clone it or simply copy the code below. In this case the database resides in the same folder as the application executable using a Windows Form project and will work also in a Control project by altering the code a little.
Important
The table name and column name is not my choice was done for another forum question.
Class for data operations
Note the connection string as a public variable, could also be private or protected.
using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Access_mdb
{
public class DataOperations
{
public static string ConnectionString =
"Provider=Microsoft.Jet.OLEDB.4.0;" +
$"Data Source={Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "db1.mdb")};";
public static List<string> Read()
{
var list = new List<string>();
using (var cn = new OleDbConnection() {ConnectionString = ConnectionString})
{
using (var cmd = new OleDbCommand() {Connection = cn})
{
cmd.CommandText = "SELECT [Consonant Determiner Required] FROM DeterminersWithA;";
cn.Open();
var reader = cmd.ExecuteReader();
while (reader.Read())
{
list.Add(reader.GetString(0));
}
}
}
return list;
}
}
}
namespace Access_mdb
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void LoadDataButton_Click(object sender, EventArgs e)
{
var cb = new ComboBox()
{
Left = 10,
Top = 10,
DataSource = DataOperations.Read()
};
Controls.Add(cb);
}
}
}
Data class modified
Here instead of doing a list the data is displayed to the console.
public static void Read()
{
using (var cn = new OleDbConnection() { ConnectionString = ConnectionString })
{
using (var cmd = new OleDbCommand() { Connection = cn })
{
cmd.CommandText = "SELECT [Consonant Determiner Required] FROM DeterminersWithA;";
cn.Open();
var reader = cmd.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(reader.GetString(0));
}
}
}
}
Note
Unless there is a really good reason for using .mdb move up to .accdb or better yet use SQL-Server LocalDb or SQL-Server Express edition.