Here’s a sample code in C# using the .NET framework to write data from an Excel file to a SQLite database
using System; using System.Data.SQLite; using System.Data.OleDb; using System.IO; namespace SqliteDatabaseExcelWriterExample { class Program { static void Main(string[] args) { // Define the connection string for the SQLite database string connectionString = "Data Source=database.db;Version=3;"; // Define the path to the Excel file string excelFilePath = "customers.xlsx"; // Define the connection string for the Excel file string excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelFilePath + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\""; // Define the name of the worksheet in the Excel file string worksheetName = "Customers"; // Define the SQL statement to create a table in the SQLite database string createTableSql = "CREATE TABLE IF NOT EXISTS Customers (FirstName TEXT, LastName TEXT, Email TEXT)"; // Execute the SQL statement to create the table in the SQLite database using (SQLiteConnection connection = new SQLiteConnection(connectionString)) { connection.Open(); using (SQLiteCommand command = new SQLiteCommand(createTableSql, connection)) { command.ExecuteNonQuery(); } } // Read the data from the Excel file and insert it into the SQLite database using (OleDbConnection excelConnection = new OleDbConnection(excelConnectionString)) { excelConnection.Open(); // Create a new instance of the OleDbCommand class using (OleDbCommand command = new OleDbCommand("SELECT * FROM [" + worksheetName + "$]", excelConnection)) { using (OleDbDataReader reader = command.ExecuteReader()) { using (SQLiteConnection connection = new SQLiteConnection(connectionString)) { connection.Open(); // Create a new instance of the SQLiteCommand class using (SQLiteCommand insertCommand = new SQLiteCommand(connection)) { // Loop through the rows of data in the Excel file while (reader.Read()) { // Define the SQL statement to insert data into the SQLite database string sql = "INSERT INTO Customers (FirstName, LastName, Email) VALUES ('" + reader["FirstName"] + "', '" + reader["LastName"] + "', '" + reader["Email"] + "')"; // Set the SQL statement of the SQLiteCommand object insertCommand.CommandText = sql; // Execute the SQL statement to insert data into the SQLite database int rowsAffected = insertCommand.ExecuteNonQuery(); } } } } } } Console.WriteLine("Data has been written to the SQLite database."); Console.ReadLine(); } } }
In this example, the OleDbConnection
class is used to create a new instance of the connection to the Excel file. The Open
method of the OleDbConnection
object is used to open the connection. The OleDbCommand
class is used to create a new instance of the SQL command to select data from the Excel file. The ExecuteReader
method of the OleDbCommand
object is used to execute the SQL statement and read the data from the Excel file. The SQLiteConnection
class is used to create a new instance of the SQLite connection to the SQLite database. The Open
method of the SQLiteConnection
object is used to open the connection. The SQLiteCommand
class is used to create a new instance of the SQL command to insert data into the SQLite database. The ExecuteNonQuery
method of the SQLiteCommand
object is used