如何从数据库中获取数据?

问题描述:

I am writing to get that out put following code
[DataContract]
    public class Employee
    {
        [DataMember]
       public string ID {get;set;}
[DataMember]
           public List<Dept> Depts;
}
 public class Dept
    {      

        [DataMember]
        public string Job { get; set; }
        [DataMember]
        public string DeptName { get; set; }
}

public List<Empoyee> AllEmp()
        {           

            List<Empoyee> lists = new List<Empoyee>();
            Empoyee dto = new Empoyee();            

            SqlConnection con = new SqlConnection(@"data");
            con.Open();
            SqlCommand cmd = new SqlCommand("selectEmpoyee", con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            adp.Fill(dt);           
            List<Dept> uplist = new List<dept>();
            Dept up;   
     intId = 0;
            dto.ID = Convert.ToInt32(dt.Rows[0]["ID"]);
            foreach (DataRow stoup in dt.Rows)
            {
                if(Id!=0)
                    loadId = Convert.ToInt32(stoup["ID"]);

                if (Id != dto.ID || (Id == 0))
                {    
                    dto = new Dept();
                    dto.ID = Convert.ToInt32(stoup["ID"]);
                    dto.Name = stoup["Name"].ToString();
                    lists.Add(dto);                   
                }               
                   
                if ((Id == dto.ID) || (Id == 0))
                {
                    up = new Dept();
                    up.Job = stoup["Job"].ToString();
                    up.DeptName = stoup["DeptName"].ToString();                   
                    
                    uplist.Add(up);                    
                                
                Id = Convert.ToInt32(stoup["ID"]);
                    }               
                dto.Depts = uplist;
            }
            return lists;
        }
but I am running this code getting following output
ID    Name   Job       DeptName    Loc

1     a       Mgr      sales         USA

1      a       xxx     ffff           UK
2     b       pro      or            USA

2      b       Dev     pur          UK
<Employees>
<employee>
<ID>1</ID>

<NAME>a</NAME>

 <DEPTS>

  <Dept>

  <Job>mgr</Job>

  <DeptName>sales</DeptName>

  <Dept>

  <Dept>

  <Job>xxx</Job>

  <DeptName>fff</DeptName>

  <Dept>
<Dept>

  <Job>Pro</Job>

  <DeptName>or</DeptName>

  <Dept>

  <Dept>

  <Job>dev</Job>

  <DeptName>pur</DeptName>

  <Dept>
<employee>
<Employee>
<ID>2</ID>

<NAME>b</NAME>

 <DEPTS>

  <Dept>

  <Job>mgr</Job>

  <DeptName>sales</DeptName>

  <Dept>

  <Dept>

  <Job>xxx</Job>

  <DeptName>fff</DeptName>

  <Dept>
<Dept>

  <Job>Pro</Job>

  <DeptName>or</DeptName>

  <Dept>

  <Dept>

  <Job>dev</Job>

  <DeptName>pur</DeptName>

  <Dept>

</Employee>
 </DEPTS>

<EMPLOYEES>
But i want to below output
<Employees>
<employee>

<ID>1</ID>

<NAME>a</NAME>

 <DEPTS>

  <Dept>

  <Job>mgr</Job>

  <DeptName>sales</DeptName>

  <Dept>

  <Dept>

  <Job>xxx</Job>

  <DeptName>fff</DeptName>

  <Dept>


<employee>
<Employee>
<ID>2</ID>

<NAME>b</NAME>

 <DEPTS>  

<Dept>

  <Job>Pro</Job>

  <DeptName>or</DeptName>

  <Dept>

  <Dept>

  <Job>dev</Job>

  <DeptName>pur</DeptName>

  <Dept>

</Employee>
 </DEPTS>

<EMPLOYEES>
At the time of debugging code executing correct but displaying xml output not getting correct
What is the mistake in my code plz correct it



anilbabu



anilbabu

您说XML是错误的,但是您没有提供任何代码来说明如何生成XML XML.您所有的代码所要做的就是从数据库中获取数据并创建自定义对象.除非您需要自定义对象,否则没有 这样做的好处.请仅张贴您用于生成XML的代码,并确定XML中到底有什么不正确.

You said the XML is wrong but you didn't provide us any code demonstrating how you're generating the XML.  All your code is doing is getting data from the database and creating custom objects.  Unless you need the custom objects then there is no benefit in doing that.  Please post just the code you're using to generate the XML and identify what exactly in the XML is not correct.

迈克尔·泰勒
http://blogs.msmvps.com/p3net

Michael Taylor
http://blogs.msmvps.com/p3net