Sök…


SQL-datakälla

Kontroller som kan bindas med data kan använda SqlDataSource kontroller. Med SqlDataSource kontrollen kan du inte bara hämta data från en databas utan också redigera och sortera data.

Hämtar data

Lagrad procedur:

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

SQL-fråga:

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

parametrar:

<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>

Var medveten om alternativet CancelSelectOnNullParameter , att om satt till true (standard) kommer att stoppa databindningen om någon parameter är NULL

Grundläggande användning

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>

Objektdatakälla

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

I koden bakom

Data Access Class

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
    }        
}

Medarbetarklassen

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


Modified text is an extract of the original Stack Overflow Documentation
Licensierat under CC BY-SA 3.0
Inte anslutet till Stack Overflow