简体   繁体   English

将Excel导入数据库时​​如何添加额外的列?

[英]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? 我正在将excelsheet导入sql服务器数据库中,在excel中有一个名为FlightNumber的列,其格式为AI-2000,AI-2564等,我在数据库中有FlightNumber列,但还有另一个名为FlightId的列,该列应具有从excel的值派生的值flightnumber(通过删除前3个字符)例如:如果excel中有FlightNumber:AI-2032,则db中的FlightId值应为2032。我应该怎么做? I am a beginner so you may have to explain along with some code. 我是一个初学者,所以您可能需要解释一些代码。

This is how i am reading excel : 这就是我阅读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 我也正在使用SqlBulkCopy与列映射一起导入

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 这里的第三个参数是DataColumn.Expression ,它根据您现有的列值指定默认值,以在此处了解有关此访问的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用SqlBulkCopy将其导入数据库时​​,如何在excel中跳过多余的行? - How can i skip extra rows in excel while importing it into database using SqlBulkCopy? 当我从数据库中获取数据时,如何在datagridview中添加额外的列 - How can I add extra column in datagridview when I fetch data from database 如何将数据添加到允许NULL的数据库列中? - How can I add data into a database column that allows NULL? 从控制器中的excel导入数据时,如何检查excel文件中是否存在特定的列? - While importing data from excel in the controller how to check if the specific column exists in the excel file or not? 使用 ASP.NET MVC 将文件从 Excel 导入 SQL 服务器时如何匹配列 ID - How to match column id while importing file from Excel into SQL Server using ASP.NET MVC 生成excel表时如何为列值添加颜色? - How to add color for column values while generating excel sheet? 如何向XAML网格添加额外的控件? - How can I add an extra control to a XAML Grid? 从Excel工作表导入数据时更改默认列标题 - change default column header while importing data from an excel sheet 日期列在C#中导入Excel时出错 - Date column Error while importing excel in c# 将Excel导入ASP.NET时进行OleDbDataAdapter和列转换 - OleDbDataAdapter and column casting while importing Excel to ASP.NET
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM