[英]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.