FrmSqlDataReader.aspx |
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server">
<title></title> </head> <body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="ctlCategoryList" runat="server">
</asp:GridView>
</div>
</form> </body> </html> |
FrmSqlDataReader.aspx.cs |
protected void Page_Load(object
sender, EventArgs
e) {
// 폼이 처음로드할 때에만 Select하자...
if (!Page.IsPostBack)
{
DisplayData();
} } private void DisplayData() {
SqlConnection con =
new SqlConnection( "server=WINDOWS-XP\\SQLSERVER;database=Test;uid=Test;pwd=1234;");
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "Select * From
Categories";
cmd.CommandType = CommandType.Text;
//[1] 데이터 조회 : Select문은 레코드를 받아와야하기때문에...
SqlDataReader dr =
cmd.ExecuteReader(); // Reader() 메서드 실행 후 결과값담기
//[2] GridView와 같은 데이터 컨트롤에 바인딩
this.ctlCategoryList.DataSource
= dr; // List<T>, DataReader, DataSet, ...
this.ctlCategoryList.DataBind();
// 실행(바인딩)
//[3] DataReader는 반드시 Close() 해주어야 한다...
dr.Close();
con.Close(); } |
결과화면 |
[그림5-1] |
'ASP.NET' 카테고리의 다른 글
07.ADO.NET - SqlError (0) | 2009.09.24 |
---|---|
06.ADO.NET - SqlDataReaderRead (0) | 2009.09.24 |
04.ADO.NET - SqlCommand (0) | 2009.09.24 |
03.ADO.NET - SqlException (0) | 2009.09.24 |
02.ADO.NET - SqlConnectionBuilder (0) | 2009.09.24 |
Comments