简体   繁体   中英

How can i add a extra column while importing excel into database?

I am importing an excelsheet into sql server database there is a column in excel named FlightNumber which is in format AI-2000, AI-2564 etc I have FlightNumber column in database but there is also another column called FlightId which should have values derived from excel's flightnumber (by removing the first 3 character) For eg: if the excel has FlightNumber: AI-2032 the FlightId value in db should be 2032. How am i supposed to do this? I am a beginner so you may have to explain along with some code.

This is how i am reading excel :

DataTable dt7 = new DataTable();
dt7.Load(dr);
DataRow[] ExcelRows = new DataRow[dt7.Rows.Count];
DataColumn[] ExcelColumn = new DataColumn[dt7.Columns.Count];

DataRow[] ExcelRows = new DataRow[dt7.Rows.Count];
//=================================================
for (int i1 = 0; i1 < dt7.Rows.Count; i1++)
 {
   if (dt7.Rows[i1]["PP NO"] == null)
      dt7.Rows[i1]["PP NO"] = 0;
 }

also i am using SqlBulkCopy to import along with column mapping

Try this

DataTable dtone = new DataTable();
dtone.Columns.Add("idNumber");
dtone.Columns.Add("idOnly", typeof(string), " substring(idNumber, 4, 100)");

dtone.Rows.Add("AI-2000");
dtone.Rows.Add("AE-2001");
dtone.Rows.Add("AF-2002");
dtone.Rows.Add("AH-2003");

Here 3rd parameter is DataColumn.Expression which specify the default value according to your per-existing columns value, to know more about this visit here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM