问题描述
我仍在尝试将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文件