Recommend creating a class for your database work.
In the class, have a private scoped variable for the connection to the database.
Create methods to populate ComboBox and other controls.
public static string ConnectionString =>
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=NorthWind.accdb";
Create a method to execute your SQL, below is a sample. Note each value for inserts need to be parameters off the OleDbCommand. The return value is the new primary key for this example, yours will be different. And your WHERE value will be a parameter.
public static (int identifier, Exception exception) SampleInsert(string companyName, string contactName)
{
using var cn = new OleDbConnection { ConnectionString = ConnectionString };
using var cmd = new OleDbCommand() { Connection = cn };
cmd.CommandText =
"INSERT INTO Customers (CompanyName,ContactName) Values (@CompanyName,@ContactName)";
cmd.Parameters.Add("@CompanyName",
OleDbType.LongVarChar).Value = companyName;
cmd.Parameters.Add("@ContactName",
OleDbType.LongVarChar).Value = contactName;
try
{
cn.Open();
cmd.ExecuteNonQuery();
return ((int)cmd.ExecuteScalar(), null);
}
catch (Exception ex)
{
return (-1, ex);
}
}
Example for reading data for ComoBox controls, make sure to set DisplayMember for the ComboBox, in this case it would be to CategoryName
public static DataTable CategoriesDataTable()
{
DataTable table = new ();
using var cn = new OleDbConnection { ConnectionString = ConnectionString };
using var cmd = new OleDbCommand() {Connection = cn};
cmd.CommandText = "SELECT CategoryID, CategoryName FROM Categories;";
cn.Open();
table.Load(cmd.ExecuteReader());
return table;
}