【发布时间】:2017-12-19 17:35:21
【问题描述】:
使用 ASP.NET MVC 将 excel 数据插入到 sql server 表中。
此行控制器出错:products.WriteToServer(_product);
这是错误信息:
来自数据源的String类型的给定值不能 转换为指定目标列的int类型。
控制器代码:
[HttpPost]
public ActionResult Index(HttpPostedFileBase postedFile)
{
string filePath = string.Empty;
if (postedFile != null)
{
string path = Server.MapPath("~/Uploads/");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
filePath = path + Path.GetFileName(postedFile.FileName);
string extension = Path.GetExtension(postedFile.FileName);
postedFile.SaveAs(filePath);
string conString = string.Empty;
switch (extension)
{
case ".xls": //Excel 97-03.
conString = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
break;
case ".xlsx": //Excel 07 and above.
conString = ConfigurationManager.ConnectionStrings["Excel07ConString"].ConnectionString;
break;
}
DataTable dt = new DataTable();
// DataTable _dt = new DataTable();
// DataTable optionCategories = new DataTable();
// DataTable options = new DataTable();
DataTable _product = new DataTable();
conString = string.Format(conString, filePath);
using (OleDbConnection connExcel = new OleDbConnection(conString))
{
using (OleDbCommand cmdExcel = new OleDbCommand())
{
using (OleDbDataAdapter odaExcel = new OleDbDataAdapter())
{
cmdExcel.Connection = connExcel;
//Get the name of First Sheet.
connExcel.Open();
DataTable dtExcelSchema;
dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
// string sheetName1 = dtExcelSchema.Rows[1]["TABLE_NAME"].ToString();
connExcel.Close();
//Read Data from First Sheet.
connExcel.Open();
cmdExcel.CommandText = "SELECT * From [" + sheetName + "]";
odaExcel.SelectCommand = cmdExcel;
odaExcel.Fill(_product);
// cmdExcel.CommandText = "SELECT * From [" + sheetName1 + "]";
//odaExcel.Fill(_dt);
connExcel.Close();
}
}
}
conString = ConfigurationManager.ConnectionStrings["Constring"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlBulkCopy products = new SqlBulkCopy(con))
{
//Set the database table name.
products.DestinationTableName = "dbo.Products";
// [OPTIONAL]: Map the Excel columns with that of the database table
products.ColumnMappings.Clear();
products.ColumnMappings.Add("Id", "Id");
products.ColumnMappings.Add("Sku", "Sku");
products.ColumnMappings.Add("UPC", "UPC");
products.ColumnMappings.Add("Name", "Name");
products.ColumnMappings.Add("Price", "Price");
products.ColumnMappings.Add("CostPrice", "CostPrice");
products.ColumnMappings.Add("RetailPrice", "RetailPrice");
products.ColumnMappings.Add("SalePrice", "SalePrice");
products.ColumnMappings.Add("Weight", "Weight");
products.ColumnMappings.Add("Quantity", "Quantity");
products.ColumnMappings.Add("Description", "Description");
products.ColumnMappings.Add("Keywords", "Keywords");
products.ColumnMappings.Add("TaxClassId", "TaxClassId");
products.ColumnMappings.Add("IsFeatured", "IsFeatured");
products.ColumnMappings.Add("IsVisible", "IsVisible");
products.ColumnMappings.Add("AddQuantity", "AddQuantity");
products.ColumnMappings.Add("IsCustomOptionProduct", "IsCustomOptionProduct");
// products.ColumnMappings.Add("DrowingRefNo", "DrowingRefNo");
con.Open();
products.WriteToServer(_product);
con.Close();
}
}
}
return View();
}
}
}
---------sql服务器表脚本----------
CREATE TABLE [dbo].[Products](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Sku] [nvarchar](100) NULL,
[UPC] [nvarchar](max) NULL,
[Name] [nvarchar](1000) NOT NULL,
[Price] [decimal](18, 2) NOT NULL,
[CostPrice] [decimal](18, 2) NULL,
[RetailPrice] [decimal](18, 2) NULL,
[SalePrice] [decimal](18, 2) NULL,
[Weight] [decimal](18, 2) NOT NULL,
[Quantity] [int] NULL,
[Description] [nvarchar](max) NULL,
[Keywords] [nvarchar](2000) NULL,
[TaxClassId] [int] NULL,
[IsFeatured] [bit] NOT NULL,
[IsVisible] [bit] NOT NULL,
[AddQuantity] [int] NULL,
[IsCustomOptionProduct] [bit] NOT NULL DEFAULT ((0)),
CONSTRAINT [PK_dbo.Products] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE [dbo].[Products] WITH CHECK ADD CONSTRAINT [FK_dbo.Products_dbo.TaxClasses_TaxClassId] FOREIGN KEY([TaxClassId])
REFERENCES [dbo].[TaxClasses] ([Id])
GO
ALTER TABLE [dbo].[Products] CHECK CONSTRAINT `enter code here`[FK_dbo.Products_dbo.TaxClasses_TaxClassId]
GO
【问题讨论】:
标签: c# asp.net-mvc