더북(TheBook)

▼  FrmSqlDataSource.aspx

<%@ Page Language="C#" AutoEventWireup="true"
  CodeBehind="FrmSqlDataSource.aspx.cs"
  Inherits="DevDataControl.FrmSqlDataSource" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <title>SqlDataSource 컨트롤</title>
</head>
<body>
<form id="form1" runat="server">
<div>
  <asp:DropDownList ID="lstMemoName" runat="server"
      DataSourceID="sdsMemoName" DataTextField="Name" DataValueField="Num">
  </asp:DropDownList>
  <asp:SqlDataSource ID="sdsMemoName" runat="server"
      ConnectionString="<%$ ConnectionStrings:DevADONETConnectionString %>"
      SelectCommand="SELECT [Num], [Name] FROM [Memos] ORDER BY [Num] DESC">
  </asp:SqlDataSource>
</div>
</form>
</body>
</html>

 

SqlDataSource 컨트롤의 ConnectionString 속성에는 <%$ %> 구문을 사용해 Web.config 파일에 저장된 데이터베이스 연결 문자열인 DevADONETConnectionString 항목을 지정하는데, ConnectionStrings:데이터베이스연결문자열 형식으로 설정한다. SelectCommand 속성에는 앞서 도구를 사용해서 지정한 SQL 구문이 설정된다.

이렇게 만들어진 SqlDataSource 컨트롤은 DropDownList와 같은 컨트롤의 DataSourceID 속성에 ID 값이 지정되어 사용된다.