我仍在尝试将excel文件上传到我的SQL工作台数据库中,而我为导入这些文件而编写的代码有效,并且实际上导入了excel文件并将其保存到我指定的文件夹中。但是,但是当我检查我的数据库时,里面什么也没有,并且我也收到了上面提到的错误。任何建议或建议都会有很大帮助。以下是我的代码。谢谢。
模型数据类在这里
命名空间air_traffic_weather.Models { 公共类数据 { [键]
public int Id { get; set; }
public string Ident { get; set; }
public string type { get; set; }
public string name { get; set; }
public int latitude_deg { get; set; }
public int longitude_deg { get; set; }
public int elevation_ft { get; set; }
public string continent { get; set; }
public string iso_country { get; set; }
public string iso_region { get; set; }
public string municipality { get; set; }
public string schedule_service { get; set; }
public string gps_code { get; set; }
public string iata_code { get; set; }
public string local_code { get; set; }
public string home_link { get; set; }
public string wikipedia_link { get; set; }
public string keywords { get; set; }
public DateTime CreatedAt { get; set; } = DateTime.Now;
public DateTime Updated { get; set; } = DateTime.Now;
}
}
家庭控制器
[HttpPost("Index")]
public IActionResult Index(IFormFile file)
{
if (file != null)
{
// giving the path where imma store my file that will be uploaded and they will be stored in a folder in made in the Root Path.
string thePath = Path.Combine(this._hostEnvironment.WebRootPath, "ExcelData");
// now checking to see if that directory already exists, if it does we don't get but if it ain't then we create one
if (!Directory.Exists(thePath))
{
Directory.CreateDirectory(thePath);
}
// now it is time to save the excel file that we gonna upload yeah!!
string theFileName = Path.GetFileName(file.FileName);
string theFilePath = Path.Combine(thePath, theFileName);
using (FileStream theStream = new FileStream(theFilePath, FileMode.Create))
{
file.CopyTo(theStream);
}
// let's also read the connection string for the excel file.
string conString = "@Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties= 'Excel 12.0 Xml;HDR=YES;'";
DataTable thedataTable = new DataTable();
conString = string.Format(conString, theFilePath);
OleDbConnection connectToExcel = new OleDbConnection(conString);
OleDbCommand commandExcel = new OleDbCommand();
OleDbDataAdapter adapter = new OleDbDataAdapter();
commandExcel.Connection = connectToExcel;
// get the first excel sheet name
connectToExcel.Open();
DataTable dtExcelSchema;
dtExcelSchema = connectToExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
connectToExcel.Close();
// read data from first sheet
connectToExcel.Open();
commandExcel.CommandText = "SELECT * FROM [" + sheetName + "]";
adapter.SelectCommand = commandExcel;
adapter.Fill(thedataTable);
connectToExcel.Close();
conString = this._configuration.GetConnectionString("cs");
using (SqlConnection connectionToSql = new SqlConnection(conString))
{
using (SqlBulkCopy hugeCopy = new SqlBulkCopy(connectionToSql))
{
// here i will set my DB table name
hugeCopy.DestinationTableName = "Datas";
// HERE IMMA GO AHEAD AND THE EXCEL COLUMNS WITH THAT OF MY DB
hugeCopy.ColumnMappings.Add("Id", "Id");
hugeCopy.ColumnMappings.Add("Ident", "Ident");
hugeCopy.ColumnMappings.Add("type", "type");
hugeCopy.ColumnMappings.Add("name", "name");
hugeCopy.ColumnMappings.Add("latitude_degrees", "latitude_degrees");
hugeCopy.ColumnMappings.Add("longitude_degrees", "longitude_degrees");
hugeCopy.ColumnMappings.Add("elevation_feet", "elevation_feet");
hugeCopy.ColumnMappings.Add("continent", "continent");
hugeCopy.ColumnMappings.Add("iso_region", "iso_region");
hugeCopy.ColumnMappings.Add("municipality", "municipality");
hugeCopy.ColumnMappings.Add("schedule_service", "schedule_service");
hugeCopy.ColumnMappings.Add("gps_code", "gps_code");
hugeCopy.ColumnMappings.Add("iata_code", "iata_code");
hugeCopy.ColumnMappings.Add("local_code", "local_code");
hugeCopy.ColumnMappings.Add("home_link", "home_link");
hugeCopy.ColumnMappings.Add("wikipedia_link", "wikipedia_link");
hugeCopy.ColumnMappings.Add("keywords", "keywords");
connectionToSql.Open();
hugeCopy.WriteToServer(thedataTable);
connectionToSql.Close();
}
}
下面的csproj文件