2013-11-05 6 views
0

저는 C#을 처음 사용합니다. Razor 뷰에 SQL을 어떻게 표시합니까?결과를보기로 가져 오는 방법

// GET: /ReportData 
public ActionResult Index() 
{ 
    using (SqlConnection connection = new SqlConnection(@"Data Source=xxx.xxx.xxx.xxx;Initial Catalog=xxxxxx;Persist Security Info=True;User ID=xxxxx;Password=xxxxx")) 
    { 
     using (SqlCommand command = connection.CreateCommand()) 
     { 
      command.CommandText = @"select * from ProjectManagement as p 
      join Implementation as i on p.JobNumber = i.JobNumber 
      join Divisions as d on i.Division = d.Division 
      where p.ProjectStatus = 'Active'" 

      connection.Open(); 
      using (SqlDataReader reader = command.ExecuteReader()) 
      { 
       while (reader.Read()) 
       { 
        // process result 
        reader.GetValue(0); 
        reader.GetValue(1); 
        reader.GetValue(2); 
        reader.GetValue(3); 
        reader.GetValue(4); 
        reader.GetValue(5); 
       } 

      } 
     } 
    }   

    return View(); 
}  

본인은 독자 객체에 따라 다르지만 이후에는 진행 방법에 대한 단서가 없습니다. 요점이 필요합니다 ...

+0

처음 사용 더미 데이터가보기에 디스플레이된다. 그러면 SQL에서 결과를 얻고 그 결과를 표시하는 방법을 이해할 것입니다. – Haritha

답변

0

정말 Entity Framework를 살펴보고 모델링해야합니다. 이 질문은 대답을 많이 요구합니다.

Microsoft의 EF (Entity Framework) http://msdn.microsoft.com/en-us/data/ef.aspx 물론 stackoverflow를 사용하고 엔티티 프레임 워크를 찾고 면도기로 모델을 봅니다.

위의 대답은 당신이 이것을 사용하고 있다고 가정하고 당신이하고있는 것처럼 데이터베이스를 돌면서 이전의 방법으로는 작동하지 않을 것입니다. 이것은 실제로 많은 답변이 아니지만 Entity Framework 또는 다른 ORM (Object Relational Mapper)에 대해 수행 할 몇 가지 조사가 있습니다.

// GET: /ReportData 
public ActionResult Index() 
{ 
    List<DataHolder> data = new List<DataHolder>(); 
    using (SqlConnection connection = new SqlConnection(@"Data Source=xxx.xxx.xxx.xxx;Initial Catalog=xxxxxx;Persist Security Info=True;User ID=xxxxx;Password=xxxxx")) 
    { 

     using (SqlCommand command = connection.CreateCommand()) 
     { 
      command.CommandText = @"select * from ProjectManagement as p 
      join Implementation as i on p.JobNumber = i.JobNumber 
      join Divisions as d on i.Division = d.Division 
      where p.ProjectStatus = 'Active'" 

      connection.Open(); 
      using (SqlDataReader reader = command.ExecuteReader()) 
      {     
       while (reader.Read()) 
       { 
        data.Add(new DataHolder { 
         Item1 = reader.GetValue(0), 
         Item2 = reader.GetValue(1) 
        }); 
        // process result 
        //reader.GetValue(0); 
        //reader.GetValue(1); 
        //reader.GetValue(2); 
        //reader.GetValue(3); 
        //reader.GetValue(4); 
        //reader.GetValue(5); 
       } 

      } 
     } 
    }   

    return View(data); 
} 


public class DataHolder { 
    public string Item1 { get; set; } 
    public string Item2 { get; set; } 
} 

이어서 view.cshtml에

@model List<DataHolder> 

@foreach(var a in Model) { 
    <p>@a.Item1 @a.Item2</p> 
} 
+1

여기에 ORM을 사용할 필요는 없습니다. 그러나, 그것은 바람직합니다. – ediblecode

+0

점핑 코드가 맞습니다. 방금 내 반응을 읽었으며 ORM을 사용해야하는 것처럼 들리게 만들었습니다. 내가 게시 한 코드는 ORM을 사용하지 않고 현재 코드로 데이터를 연결하는 방법을 대략적으로 보여줍니다. –