问题
Using c# and sqlite
Pretty much exactly as the title. I want to insert into a database an excel sheet that has a different number of columns than the database. I suspect I need to use an if case to check if the columns are as the same as the db but I'm not sure what the line of code for it would be. Mostly everything I have found are different to what I'm looking for. I can get the data set to display the excel based on the bottom but
private void ImportExcelbutton_Click(object sender, EventArgs e)
{
OpenFileDialog OpenFileDialog1 = new OpenFileDialog();
if (OpenFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
try
{
this.filenametext.Text = OpenFileDialog1.FileName;
using (var stream = File.Open(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read))
{
using (IExcelDataReader reader = ExcelReaderFactory.CreateReader(stream))
{
DataSet result = reader.AsDataSet(new ExcelDataSetConfiguration()
{
ConfigureDataTable = (_) => new ExcelDataTableConfiguration() { UseHeaderRow = true }
});
tableCollection = result.Tables;
cboSheet.Items.Clear();
foreach (DataTable table in tableCollection)
cboSheet.Items.Add(table.TableName);
}
}
}
catch
{
MessageBox.Show("Error file is open/in use");
}
}
for(i=0;i<13;i++)
{
if(datagridview1.Columns[i] != dataset)
{
//code
{
}
}
来源:https://stackoverflow.com/questions/59817437/insert-into-database-from-excel-sheet-that-has-different-no-of-columns