in c# sqlite project, I need to import a csv file into my table, but i don't know how to…
- User
in c# sqlite project, I need to import a csv file into my table, but i don't know how to write the transaction code to insert my data, I have this code so far:
System.Data.DataTable dataTable = new System.Data.DataTable();
foreach (string line in File.ReadLines(filePath)) { if (line != "") { if (headers) { headers = false; // Split the line into columns columns = line.Split('|'); foreach (string column in columns) { dataTable.Columns.Add(column); } } else { // Split the line into values string[] values = line.Split('|'); // Create a new DataRow and populate it with values System.Data.DataRow row = dataTable.NewRow(); for (int i = 0; i < values.Length; i++) { row[i] = values[i].Trim('\''); } // Add the DataRow to the DataTable dataTable.Rows.Add(row); } } } using (SQLiteTransaction transaction = sqliteConn.BeginTransaction()) { using (SQLiteDataAdapter adapter = new SQLiteDataAdapter()) { // TODO: MAKE THE INSERT STATEMENT // Use the DataTable as the source for the insert operation adapter.Update(dataTable); } transaction.Commit(); } - Assistant
- User
NO, CONSIDER i DON'T KNOW THE DATA TYPES BEFOREHAND, I only have the headers
- Assistant