我有一个excel文件,我需要将其插入到sqlserver中,所以我得到这个代码来插入这是插入的方法:
private void InsertDaily_Inventory(string FilePath)
{
try {
using (SqlConnection conn = new SqlConnection("Data Source=(local);Database='SimDevice_Stocks';Integrated Security=yes;"))
{
conn.Open();
SqlCommand cmd = new SqlCommand("Delete Temp_DailyInventory", conn);
cmd.ExecuteNonQuery();
//SqlCommand cmd1 = new SqlCommand("alter table Temp_DailyInventory alter column Order_Date nvarchar(200)", conn);
//cmd1.ExecuteNonQuery();
conn.Close();
}
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' OR ORDER_SUB_TYPE='New') and ORDER_STATUS='Complete' and (ROOT_PRODUCT_ATM_TYPE = 'Prepaid' OR ROOT_PRODUCT_ATM_TYPE = 'Postpaid') and STORE_ID Like 'S' ", "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];
//connection();
//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");
//inserting Datatable Records to DataBase
con.Open();
objbulk.WriteToServer(Exceldt);
using (SqlCommand cmd3 = new SqlCommand("UPDATE Temp_DailyInventory SET Order_Date = LEFT(Order_Date, CHARINDEX(' ', Order_Date) - 1) WHERE CHARINDEX(' ', Order_Date) > 0", con))
{
cmd3.ExecuteNonQuery();
}
con.Close();
}
catch(Exception ex)
{
}
}
然后在按钮上单击它会插入记录
protected void InsertInventoryFile_Click(object sender, EventArgs e)
{
try
{
// string CurrentFilePath = Path.GetFullPath(FileUpload1.PostedFile.FileName);
//InsertDaily_Inventory(CurrentFilePath);
if (FileUpload1.PostedFile != null)
if (FileUpload1.FileName.EndsWith(".xlsx"))
{
try
{
if (!Directory.Exists(Server.MapPath("~/ExcelFiles/InventoryFiles/")))
Directory.CreateDirectory(Server.MapPath("~/ExcelFiles/InventoryFiles/"));
string fileName = Guid.NewGuid().ToString();
string fullPath = Server.MapPath("~/ExcelFiles/InventoryFiles/") + fileName + ".xlsx";
FileUpload1.SaveAs(fullPath);
//DataTable table = Excel.Import.Query(fullPath);
InsertDaily_Inventory(fullPath);
Response.Write(@"<script langauge='javascript'>alert('Data Imported')</script>");
}
catch (Exception ex)
{
Response.Write(@"<script langauge='javascript'>alert('"+ex.Message+"')</script>");
}
}
}
catch(Exception exx)
{
}
}
因此数据包含一些数据,如SCCC387&#39;如果有任何代码可以帮助我将数据从excel插入sqlserver数据库,请不要插入它们唯一插入商店ID的数字,例如&#39; 1023&#39;如果有任何代码可以帮助我将数据从excel插入sqlserver数据库请不要不要犹豫,把它给我 提前谢谢
答案 0 :(得分:1)
使用filelocation,您可以使用以下命令为excel文件创建OleDbConnection连接:
String[] excelSheets = new String[dt.Rows.Count];
int t = 0;
//get the list of excelSheetNames
foreach (DataRow row in dt.Rows)
{
excelSheets[t] = row["TABLE_NAME"].ToString();
t++;
}
string query = string.Format("Select * from [{0}]", excelSheets[0]);
System.Data.DataSet ds = new System.Data.DataSet();
using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, excelConnection))
{
dataAdapter.Fill(ds);
}
if (ds.Tables.Count > 0)
{
DataTable tb = ds.Tables[0];
return tb;
}
然后您可以使用以下代码检查Excel的数据:
<add name="xls" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" />
<add name="xlsx" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" />
在appconfig中,您需要将connectionString提供程序设为excel:
SELECT (col1 + col2) AS "Result1",
col2 AS "Result2",
FROM
myschema.results
如果这有助于您,请告诉我。
答案 1 :(得分:0)
您正在使用批量复制,也许您应该独立处理每一行以处理可能的错误。还要验证列的类型。 excel驱动程序默认根据前8行确定类型。
我强烈建议您尽可能将SSIS用于数据记录和集成。如果您有sql server标准版,则可以使用SSIS。