c# - BULK UPLOAD ERROR “错误来自数据源的 String 类型的给定值无法转换为指定目标列的 int 类型。”
问题描述
我正在尝试使用 BulkUpload 将数据从 CSV 导入数据库,我可以复制 varchar 列,但是当我尝试将“部门”数据导入 Usertale 时无法导入。
错误:来自数据源的 String 类型的给定值无法转换为指定目标列的 int 类型。
public ActionResult ImportFromExcel(HttpPostedFileBase postedFile)
{
if (ModelState.IsValid)
{
if (postedFile != null && postedFile.ContentLength > (1024 * 1024 * 50)) // 50MB limit
{
ModelState.AddModelError("postedFile", "Your file is to large. Maximum size allowed is 50MB !");
}
else
{
string path = Server.MapPath("~/Uploads/");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
string filePath = path + Path.GetFileName(postedFile.FileName);
string extension = Path.GetExtension(postedFile.FileName);
postedFile.SaveAs(filePath);
string conString = string.Empty;
switch (extension)
{
case ".xls": //For Excel 97-03.
conString = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
break;
case ".xlsx": //For Excel 07 and above.
conString = ConfigurationManager.ConnectionStrings["Excel07ConString"].ConnectionString;
break;
}
try
{
DataTable dt = 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();
connExcel.Close();
//Read Data from First Sheet.
connExcel.Open();
cmdExcel.CommandText = "SELECT * From [" + sheetName + "]";
odaExcel.SelectCommand = cmdExcel;
odaExcel.Fill(dt);
connExcel.Close();
}
}
}
conString = ConfigurationManager.ConnectionStrings["SLOT_TESTING_DB"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
con.Open();
using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
{
//Set the database table name.
sqlBulkCopy.DestinationTableName = "UserTables";
sqlBulkCopy.ColumnMappings.Add("User_id", "User_id");
sqlBulkCopy.ColumnMappings.Add("Name", "Name");
sqlBulkCopy.ColumnMappings.Add("Email", "Email");
sqlBulkCopy.ColumnMappings.Add("UserType", "UserType");
sqlBulkCopy.ColumnMappings.Add("Department_Name", "DepartmentId");
//int dptname = sqlBulkCopy.ParseInt();
//sqlBulkCopy.DepartmentId.tryParseToDepartment_Name();
//sqlBulkCopy.ColumnMappings.Add("TermId", "TermId");
//Int32.TryParse(Department.Text, out int x);
//Convert.ToInt64("Department_Name");
sqlBulkCopy.WriteToServer(dt);
Response.Write("Bulk data stored successfully");
Console.WriteLine("File uploaded successfully");
}
}
}
//catch (Exception ex)
//{
// throw ex;
//}
catch (Exception e)
{
return Json("error" + e.Message);
}
//return RedirectToAction("Index");
}
}
//return View(postedFile);
return Json("File uploaded successfully");
//Console.WriteLine("File uploaded successfully");
}
解决方案
推荐阅读
- angular - 如何在库项目中拥有绝对导入路径?
- php - 在 Python 中使用 subprocess.run() 运行脚本,阻止所有文件读/写尝试
- mysql - 动态枢轴汇总和分组
- getstream-io - GetStream - 速率限制
- python - Python 重复整数列表
- linux - Flutter 在我自己的用户文件夹中需要 sudo
- python - 将 numpy 数组的两列相加并将其添加为数组的第三列
- python - 如何在 jupyter notebook 的选项卡式布局中延迟输出?
- swift - 如何将 Firebase 数据库附加到我的对象,从更近的项目到更远的项目?
- laravel - 如何从数据库种子访问自动递增的 id?