问题描述
[HttpPost("Index")]
public IActionResult Index(Airport file)
{
// first step is to import the excel file which is what this line of code below is doing
if (ModelState.IsValid)
{
string filePath = null;
if (file.AirportFile != null)
{
string UploadsFolder = Path.Combine(_hostEnvironment.WebrootPath,"Airports");
filePath = Guid.NewGuid().ToString() + "_" + file.AirportFile.FileName;
string theFilePath = Path.Combine(UploadsFolder,filePath);
file.AirportFile.copyTo(new FileStream(theFilePath,FileMode.Create));
Airport newFile = new Airport
{
AirportFilePath = filePath,};
Console.WriteLine(newFile);
// then we are gonna implement a connection string using microsoft.ace.oledb
// also once the file have been retrieved into a variable i named tableName,i will then create an OleDb command object and pass the select query and the connection string to it
string excelConnectionString = @"Provider='Microsoft.ACE.OLEDB.12.0';Data Source='" + UploadsFolder + "';Extended Properties='Excel 12.0 Xml;IMEX=1'";
OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
//getting excel sheet name
excelConnection.open();
string tableName = excelConnection.GetSchema("Tables").Rows[0]["TABLE_NAME"].ToString();
//then we close it
excelConnection.Close();
OleDbCommand cmd = new OleDbCommand("Select * From [" + tableName + "]",excelConnection);
excelConnection.open();
// here i am creating an OleDbDataReader that will all the records of the file
OleDbDataReader theReader;
theReader = cmd.ExecuteReader();
// once the importing process begins,i do create a variable called hugecopy and set it to be the sql DB table name to its destination table name,and then thru the hugecopy.columnmappings.add,i will be able to map thru my db
sqlBulkcopy hugecopy = new sqlBulkcopy(ConfigurationManager.ConnectionStrings["CS"].ConnectionString);
hugecopy.DestinationTableName = "Weather";
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");
hugecopy.WritetoServer(theReader);
excelConnection.Close();
}
// ViewBag.Result = "Success";
}
ViewBag.Result = "Success";
模型
命名空间air_traffic_weather.Models { 公共舱机场 {
[Key]
public int Id { get; set; }
[FileExt(Allow = ".xls,.xlsx",ErrorMessage = "Only excel file")]
public IFormFile AirportFile { get; set; }
public string AirportFilePath { get; set; }
public string Ident { get; set; }
public string type { get; set; }
public string name { get; set; }
public int latitude_degrees { get; set; }
public int longitude_degrees { get; set; }
public int elevation_feet { get; set; }
public string continent { 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;
}
}
<input type="file" name="file" />
<input type="submit" value="import to DB" class=btn-btn-primary />
</form>
这是我到目前为止所拥有的,并且我一直遇到此错误或问题。任何建议或想法都会很有用,我已经被这个问题困扰了几个小时了。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)