如何提取列表对象的属性并传递到存储过程中

kgsdhlau  于 2021-08-09  发布在  Java
关注(0)|答案(1)|浏览(307)

我有一个模范班的员工

public class Employee
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Designation { get; set; }
        public string Department { get; set; }
        public DateTime Dateofjoin { get; set; }
        public string Phone { get; set; }
        public string Address { get; set; }
        public int IsActive { get; set; }
        public int row_number { get; set; }

    }

以及具有employee模型类的list对象和另一个属性的另一个模型类

public class EmployeeList
    {
        public IList<Employee> Emp_List { get; set; }
        public int Return_Param { get; set; }
    }

我有一个数据库调用方法,该方法的输入是employeelist模型类的对象。我想将employeelist模型类中empïu list的每个属性作为参数传递给存储过程。这是密码

public int Edit_Employee(EmployeeList emp)
        {

            try
            {               
                if (con.State != ConnectionState.Open)
                    con.Open();

                IList<Employee> newlist = new List<Employee>();
                newlist = emp.Emp_List;

                using (SqlCommand cmd = con.CreateCommand())
                {
                    SqlParameter RETURN_VALUE_OUTPUT = new SqlParameter();
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    cmd.CommandText = "dbo.Proc_PIP_Employee";
                    cmd.Parameters.Add("@Flag", SqlDbType.Int).Value = 3;
                    cmd.Parameters.Add("@Empid", SqlDbType.Int).Value = newlist.Id;
                    cmd.Parameters.Add("@Name", SqlDbType.VarChar, 500).Value =;
                    cmd.Parameters.Add("@Designation", SqlDbType.VarChar, 200).Value = ;
                    cmd.Parameters.Add("@Department", SqlDbType.VarChar, 200).Value = ;
                    cmd.Parameters.Add("@DateofJoin", SqlDbType.DateTime).Value = ;
                    cmd.Parameters.Add("@Phone", SqlDbType.VarChar, 10).Value = ;
                    cmd.Parameters.Add("@Isactive", SqlDbType.Int).Value = ;

newlist.id出现错误,比如doesn不包含id的定义。如何将emp\u list的每个属性作为存储过程参数传递?

pgpifvop

pgpifvop1#

看来你只是错过了一个机会 foreach :

foreach (var emp in emp.Emp_List)
{
    // ... blah ...
    cmd.Parameters.Add("@Empid", SqlDbType.Int).Value = emp.Id;
    cmd.Parameters.Add("@Name", SqlDbType.VarChar, 500).Value = emp.Name;
    // ... blah ...
    cmd.ExecuteNonQuery();
}

相关问题