简体   繁体   中英

how to extract properties of a list object and passing into stored procedure

I have a model class Employee

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; }

    }

and another model class having list object of Employee model class along with another property

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

I have a Database call method and the input to that method is the object of EmployeeList model class. I would like to pass each property of Emp_List in EmployeeList model class as parameters to the stored procedure. here is the code

  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 gives error like doesnt contain definition for Id. How do I pass each property of Emp_List as stored procedure parameter?

It seems like you're simply missing a 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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM