开发者社区> 问答> 正文

加载视图与之前的现有数据进行编辑记录在运行update存储过程之前?

我有下面的控制器操作方法。

public int BulkUpdate(List employees) {

       Connection();
       employees = GetAllEmployees().ToList();
        // using reflection, i mean why did i use this ??
        //used it for converting the generic list into datatables.
       DataTable dt = new DataTable(typeof(Employee).Name);
       PropertyInfo[] props = typeof(Employee).GetProperties(BindingFlags.Public | BindingFlags.Instance);
       foreach (var prop in props)
       {
           dt.Columns.Add(prop.Name);
       }

       foreach (var employee in employees)
       {
           var values = new object[props.Length];
           for (int i = 0; i < props.Length; i++)
           {
               values[i] = props[i].GetValue(employee, null);
           }

           dt.Rows.Add(values);
       }
       //
       using (SqlCommand cmd = new SqlCommand("BulkUpdateEmployee", con))
       {
           cmd.CommandType = CommandType.StoredProcedure;
           SqlParameter parameter = cmd.Parameters.AddWithValue("@tblEmployeeType", dt);
           parameter.SqlDbType = SqlDbType.Structured;
           parameter.TypeName = "dbo.EmployeeType";


           con.Open();
           var rowsAffected = cmd.ExecuteNonQuery();
           con.Close();

           return rowsAffected;
       }//using ends here

   }

and this method is being called in main controller:

[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)] public JsonResult BulkUpdateOrInsert(List employees) { int rowsAffected=empRepo.BulkUpdate(employees); return Json(rowsAffected, JsonRequestBehavior.AllowGet);

   }

展开
收起
SONGYiiiD 2019-12-06 21:37:33 1449 0
0 条回答
写回答
取消 提交回答
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载