here’s a sample code in C# using the .NET framework to write data to an Access database
using System; using System.Data.OleDb; namespace AccessDBWriterExample { class Program { static void Main(string[] args) { // Define the Access database connection string string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\UserName\Documents\example.accdb"; // Define the SQL query to insert data into the database string query = "INSERT INTO ExampleTable (Name, Age, Gender) VALUES ('John Smith', 30, 'Male')"; // Open the Access database connection using (OleDbConnection conn = new OleDbConnection(connString)) { // Open the connection conn.Open(); // Create a command object with the SQL query and the connection using (OleDbCommand cmd = new OleDbCommand(query, conn)) { // Execute the SQL query int rowsAffected = cmd.ExecuteNonQuery(); Console.WriteLine("{0} rows affected.", rowsAffected); } } Console.WriteLine("Data has been written to the Access database."); Console.ReadLine(); } } }
In this example, the OleDbConnection
class is used to establish a connection to the Access database. The connection string includes the provider name and the path to the Access database file. The OleDbCommand
class is used to create a command object with the SQL query and the connection. The ExecuteNonQuery
method is used to execute the SQL query and return the number of rows affected.