从Excel工作表插入到sqlserver?

时间:2017-03-22 12:56:09

标签: c#

我试图将excel中的特定列插入到sql server上的数据库中,这里的代码如下:

constr = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES;IMEX=1;""", FilePath);
            Econ = new OleDbConnection(constr);
            SqlConnection con = new SqlConnection("Data Source=(local);Database='SimDevice_Stocks';Integrated Security= yes;");

            Query = string.Format("Select [ORDER_SUB_TYPE],[ORDER_DATE],[ORDER_STATUS],[SIM_NETWORK_TYPE],[STORE_ID],[ROOT_PRODUCT_ATM_TYPE],[STC_ERP_SIM_ITEM],[STC_SALECO_ITEM_CODE]  FROM [{0}] where ORDER_SUB_TYPE='Change SIM' and ORDER_SUB_TYPE='New'and  ORDER_STATUS = 'Complete' and ROOT_PRODUCT_ATM_TYPE='Prepaid'", "Online_Inventory_Open_Points_re$");
            OleDbCommand Ecom = new OleDbCommand(Query, Econ);
            Econ.Open();   
            DataSet ds = new DataSet();
            OleDbDataAdapter oda = new OleDbDataAdapter(Query, Econ);
            Econ.Close();
            oda.Fill(ds);
            DataTable Exceldt = ds.Tables[0];
            //creating object of SqlBulkCopy  
            SqlBulkCopy objbulk = new SqlBulkCopy(con);
            //assigning Destination table name    
            objbulk.DestinationTableName = "Temp_DailyInventory";
            //Mapping Table column    
            objbulk.ColumnMappings.Add("ORDER_SUB_TYPE", "Order_Sub_Type");
            objbulk.ColumnMappings.Add("Order_Date", "Order_Date");
            objbulk.ColumnMappings.Add("ORDER_STATUS", "Order_Status");
            objbulk.ColumnMappings.Add("SIM_NETWORK_TYPE", "Sim_Network_Type");
            objbulk.ColumnMappings.Add("STORE_ID", "Store_ID");
            objbulk.ColumnMappings.Add("ROOT_PRODUCT_ATM_TYPE", "Root_Product_Atm_Type");
            objbulk.ColumnMappings.Add("STC_ERP_SIM_ITEM", "Stc_Erp_Sim_Item");
            objbulk.ColumnMappings.Add("STC_SALECO_ITEM_CODE", "Stc_Saleco_Item_Code");

   con.Open();
   objbulk.WriteToServer(Exceldt);
   con.Close();

所以我在Order_Sub_Type的excel表格列中得到了2种类型的数据,它得到了更改SIM卡和新的所以我需要输入这2条记录,但我无法插入它们,因为你可以在上面的查询中看到我可以选择一个进入"更改模拟"或"新"。

提前致谢

0 个答案:

没有答案