[ASP.NET] ADONET : ms-sql 데이터베이스 GridView
=====================================================
FrmSqlDataAdapter.aspx
=====================================================
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FrmSqlDataAdapter.aspx.cs" Inherits="WebADONET.FrmSqlDataAdapter" %>
=====================================================
FrmSqlDataAdapter.aspx.cs
=====================================================
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebADONET
{
public partial class FrmSqlDataAdapter : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DisplayData();
}
private void DisplayData()
{
//[1] sqlonnection 클래스의 인스턴스 생성
SqlConnection con = new SqlConnection();
//[2] ConnectionString 속성지정
con.ConnectionString = "server=SUPERMAN\\MSQL;database=study;uid=study;pwd=XXXXX";
//[3] Open 메소드로 데이터베이스 연결
con.Open();
//[!] 입력예시문 실행
//sqlCommand의 인스턴스 생성
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.Connection = con;
// CommandText 속성에 모든 명령어 지정
sqlCmd.CommandText = "select * from Memos Order by Num Desc";
//CommandType 속서어 : Text, SP
sqlCmd.CommandType = CommandType.Text;
//데이터 어댑터 객체
SqlDataAdapter sqlDA = new SqlDataAdapter();
sqlDA.SelectCommand = sqlCmd;
//데이터셋
DataSet ds_Data = new DataSet();
sqlDA.Fill(ds_Data, "Memos");
//출력
gv_MemoList.DataSource = ds_Data;
gv_MemoList.DataBind();
//[4] Close 메소드로 데이터베이스 연결 종료
con.Close();
}
}
}