How to insert data in from C# to Ms Access BE

Hi

I’m now stuck with how to get the data from window forms application into the MS access Back end (BE)
The back end has the following tables in Ms Access 2007:
(1)      tblInvoices with the following controls
•      CustomerID (ComboBox)
•      SaleTime (Txt)
•      SalesMan (Txt)
(2)      Sub table (tblSaleslinedetails) linked to the tblinvoice has the following controls:
•      PronductName (ComboBox)
•      WarehouseName(ComboBox)
•      SalesQty (Txt)
•      Price (Txt)
Now the question is, how do I complete the coding in C# form to insert the data in the Back end tables above. Below is the suggested string:
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccessFile.accdb;
Jet OLEDB:Database Password=MyDbPassword;

We are required to use C# form because of the requirements to interface the POS program which require that our project in C# should have the essential files in the same directory  
Regards

Chris
LVL 2
Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.ScDirectorAsked:
Who is Participating?
 
ste5anConnect With a Mentor Senior DeveloperCommented:
[..] requirements to interface the POS program [..]
This is important: What part in the POS is your program?

Then: You normally use OleDb.

E.g.
namespace ConsoleCS
{
    using System;
    using System.Data.OleDb;

    public class Program
    {
        public static void Main(string[] args)
        {
            string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Temp\test.accdb";
            string insertSql = "INSERT INTO yourTable ( Payload ) VALUE ( 1 );";
            using (OleDbConnection connection = new OleDbConnection(connectionString))
            {
                OleDbCommand command = new OleDbCommand(insertSql);
                command.Connection = connection;
                try
                {
                    connection.Open();
                    command.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }

            Console.WriteLine("Done.");
            Console.ReadLine();
        }
    }
}

Open in new window


Caveat: do you work on a 32-bit or 64-bit machine? In the second case you need to install the ACE.OLDB 64-bit driver manually.

p.s. Asking a question like
[..] how do I complete the coding in [..]
is pretty unkind without posting your actual code. Cause concise code samples showing your problem has two advantages. 1) we have the same code base to talk about. 2) it already shows that you can abstract and isolate problems.
0
All Courses

From novice to tech pro — start learning today.