수색…


SQL 데이터 원본

데이터와 바인딩 할 수있는 컨트롤은 SqlDataSource 컨트롤을 사용할 수 있습니다. SqlDataSource 컨트롤을 사용하면 데이터베이스에서 데이터를 검색 할 수있을뿐 아니라 데이터를 편집하고 정렬 할 수 있습니다.

데이터 검색

저장 프로 시저 :

<asp:SqlDataSource ID="SqlDataSourceEmployees"
    runat="server"
    ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
    SelectCommand="sp_GetEmployees"
    SelectCommandType="StoredProcedure">
</asp:SqlDataSource>

SQL 쿼리 :

<asp:SqlDataSource ID="SqlDataSourceEmployees"
    runat="server"
    ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
    SelectCommand="SELECT
                       EmployeeID, 
                       EmployeeFirstName,
                       EmployeeLastName
                   FROM
                       dbo.Employees">
</asp:SqlDataSource>

매개 변수 :

<asp:SqlDataSource ID="SqlDataSourceEmployees"
    runat="server"
    ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
    SelectCommand="SELECT
                       EmployeeID, 
                       EmployeeFirstName,
                       EmployeeLastName
                   FROM
                       dbo.Employees
                   WHERE
                       DepartmentID = @DepartmentID;">
    <SelectParameters>
        <asp:ControlParameter ControlID="ddlDepartment"
            Name="DepartmentID"
            PropertyName="SelectedValue" />
    </SelectParameters>
</asp:SqlDataSource>

CancelSelectOnNullParameter 옵션을 알고 있어야합니다. 매개 변수가 NULL이면 true (기본값)로 설정하면 데이터 바인딩이 중지됩니다.

기본 사용법

GridView :

<asp:GridView ID="GridViewEmployees"
    runat="server"
    AutoGenerateColumns="false"
    DataSourceID="SqlDataSourceEmployees">
    <Columns>
        <asp:BoundField DataField="EmployeeID" HeaderText="Employee ID" />
        <asp:BoundField DataField="EmployeeFirstName" HeaderText="First Name" />
        <asp:BoundField DataField="EmployeeLastName" HeaderText="Last Name" />
    </Columns>
</asp:GridView>

개체 데이터 원본

<asp:ObjectDataSource ID="ObjectDataSourceEmployees" runat="server"
    TypeName="MyPackage.MyDataAccessClass"                       
    DataObjectTypeName="MyPackage.Employee" 
    SelectMethod="GetEmployees"
    UpdateMethod="SaveEmployee"
    InsertMethod="SaveEmployee">
</asp:ObjectDataSource>

코드 숨김

데이터 액세스 클래스

public class MyDataAccess
{
    public static List<Employee> GetEmployees()
    {
        List<Employee> results = new List<Employee>()
        {
            new Employee(){ Id=1, Name="John Smith" }, 
            new Employee(){ Id=2, Name="Mary Jane" } 
        };
    
        return results;
    }  

    public static void SaveEmployee(Employee e)
    {
        // Persist Employee e to the DB/cache etc. here
    }        
}

직원 클래스

public class Employee
{
    public Int32EmployeeId { get; set; }
    public string Name { get; set; }
}


Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow