ASP.NET

06.MVC DB 연결 (출력하기)

Godffs 2014. 5. 19. 04:41
반응형

이제 저장된 내용을 보는 조회 페이지를 만들려고 합니다.

먼저 이전에 저장된 내용을 보여주기 위해서 이전에 만든 USER_DAL.cs에서 코드를 추가합니다.

 USER_DAL.cs
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public static DataSet Select_User()
{
    con.Open();
 
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;
 
    cmd.CommandText = string.Format("Select *From TB_USER");
 
    cmd.CommandType = CommandType.Text;
    SqlDataAdapter da = new SqlDataAdapter();
    da.SelectCommand = cmd;
 
    DataSet ds = new DataSet();
    da.Fill(ds, "TB_USER");
    con.Close();
 
    return ds;
}

다음 화면 UI를 수정합니다.
 Index.cshtml
 

 


다음 Controller.cs 에서 코드를 추가합니다.
 GodffsController.cs
 
1
2
3
4
5
6
7
8
9
        [HttpGet]
        public ActionResult NewForm2()
        {
            DataSet ds = new DataSet();
 
            ds = USER_DAL.Select_User();
 
            return View(ds);
        }
컨트롤을 추가하고 NewForm2로 뷰 페이지를 만들어주세요.

Controller에서 DataSet으로 넘기기 때문에 View페이지에서 DataSet을 받아서 화면에 보여줘야합니다.
 NewForm2.cshtml
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
@model System.Data.DataSet
@using System.Data
 
<!DOCTYPE html>
 
<html>
<head>
    <title>NewForm2</title>
</head>
<body>
    <table>
        <tr>
            <td>id</td>
            <td>NAME</td>
        </tr>
        @foreach (DataRow row in Model.Tables["TB_USER"].Rows)
        {
            <tr>
                <td>@(row["ID"])</td>
                <td>@(row["NAME"])</td>
            </tr>
        }
    </table>
</body>
</html>
 


다음 결과 확인 합니다.

 




반응형