티스토리 뷰
* 연결기반 데이터베이스 연동
- Connection 개체 : 데이터베이스에 연결
- Command 개체 : SQL문을 실행
- DataReader 개체 : 데이터 참조
Ex) employee 테이블에서 job_id > 12 인 사원들을 출력하는 예제
* aspx 파일
// 자동으로 연결하는 경우 SqlDataSource를 이용하여 바로 연결
<h2> 연결기반 데이터베이스 연동 (SQL 자동으로 연결) </h2>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
DataKeyNames="emp_id" DataSourceID="SqlDataSource1" BackColor="White"
BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3">
<RowStyle ForeColor="#000066" />
<Columns>
<asp:BoundField DataField="emp_id" HeaderText="emp_id" ReadOnly="True"
SortExpression="emp_id" />
<asp:BoundField DataField="fname" HeaderText="fname" SortExpression="fname" />
<asp:BoundField DataField="minit" HeaderText="minit" SortExpression="minit" />
<asp:BoundField DataField="lname" HeaderText="lname" SortExpression="lname" />
<asp:BoundField DataField="job_id" HeaderText="job_id"
SortExpression="job_id" />
<asp:BoundField DataField="job_lvl" HeaderText="job_lvl"
SortExpression="job_lvl" />
<asp:BoundField DataField="pub_id" HeaderText="pub_id"
SortExpression="pub_id" />
<asp:BoundField DataField="hire_date" HeaderText="hire_date"
SortExpression="hire_date" />
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
</asp:GridView>
// 코딩으로 연결하는 경우 cs 파일에서 데이터베이스에 연결 및 데이터를 가져와서 GridView 컨트롤에 바인딩까지 해주어야 한다.
<h2> 연결기반 데이터베이스 연동 (SQL 코딩으로 연결) </h2>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:PUBSConnectionString %>"
SelectCommand="SELECT * FROM [employee] WHERE ([job_id] > @job_id)">
<SelectParameters>
<asp:Parameter DefaultValue="12" Name="job_id" Type="Int16" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None">
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
* cs 파일
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// sqlConnection 개체 생성
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["PubsConnectionString"].ConnectionString);
// sqlCommand 개체 생성
string strSql = "Select * from Employee where job_id > 12";
SqlCommand cmd = new SqlCommand(strSql, con);
// SqlDataReader 개체 생성
con.Open(); // sqlConnection 개체 오픈
SqlDataReader rd = cmd.ExecuteReader();
// sqlDataReader 개체를 GridView 컨트롤 바인딩
GridView1.DataSource = rd;
GridView1.DataBind();
// sqlDataReader 개체 및 sqlConnection 개체닫기
rd.Close();
con.Close();
}
}
}
* 결과화면
- Total
- Today
- Yesterday
- Network
- ASP.NET
- asp
- scriptmanager
- putty
- T-SQL Programming
- install
- 10g
- 시스템 함수
- Enterprise-R4-U4
- SQL SERVER 2008
- 십이지권
- 자금우
- CSS
- Java
- HTML
- VMware
- Calendar Popup
- 함수
- docuprint203a
- 자바
- SQLPLUS
- Oracle 10g
- oracle
- webserver 구축
- dbca
- Microsoft Certified IT Professional
- 자격증
- MSSQL
- enterprise linux
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
27 | 28 | 29 | 30 | 31 |