<asp:SqlDataSourceID="Countries" RunAt="server" ConnectionString="server=localhost;database=northwind;" SelectCommand="select distinct country from customers order by country"/> <asp:SqlDataSourceID="Customers" RunAt="server" ConnectionString="server=localhost;database=northwind;" SelectCommand="select * from customers where country =@Country"> <SelectParameters> <asp:ControlParameterName="Country" ControlID="MyDropDownList" PropertyName="SelectedValue"/> </SelectParameters> </asp:SqlDataSource> <asp:DropDownListID=" MyDropDownList" DataSourceID="Countries" DataTextField="country" AutoPostBack="true" RunAt="server"/> <asp:DataGridDataSourceID="Customers" RunAt="server"/>
2.7 调研存储过程例子
<asp:SqlDataSourceID="Countries" RunAt="server" ConnectionString="server=localhost;database=northwind;" SelectCommand="proc_GetCountries"/> <asp:SqlDataSourceID="Customers" RunAt="server" ConnectionString="server=localhost;database=northwind;" SelectCommand="proc_GetCustomers"> <SelectParameters> <asp:ControlParameterName="Country" ControlID="MyDropDownList" PropertyName="SelectedValue"/> </SelectParameters> </asp:SqlDataSource> <asp:DropDownListID="MyDropDownList" DataSourceID="Countries" DataTextField="country" AutoPostBack="true" RunAt="server"/> <asp:DataGridDataSourceID="Customers" RunAt="server"/> CREATE PROCEDURE proc_GetCustomers @Country nvarchar(32) AS SELECT * FROM Customers WHERE Country = @Country GO CREATE PROCEDURE proc_GetCustomers CREATE PROCEDURE proc_GetCountriesAS SELECT DISTINCT Country FROM Customers ORDER BY Country GO
7.错误检测 数据更新后控件调用的事件GridView.RowUpdated,DetailsView.ItemUpdated,SqlDataSource.Updated, etc. 处理“status”的事件,无论数据库是否异常允许数据库异常被处理或者再次抛弃,显示多少数据库行被修改 处理更新错误
<asp:SqlDataSourceID="Employees" RunAt="server" UpdateCommand="" OnUpdated="OnUpdateComplete"> </asp:SqlDataSource> void OnUpdateComplete (Object source, SqlDataSourceStatusEventsArgse) { if (e.Exception!= null) { // Exception thrown. Set e.ExceptionHandledto true to prevent // the SqlDataSourcefrom throwing an exception, or leave it set // to false to allow SqlDataSourceto rethrowthe exception } else if (e.AffectedRows== 0) { // No exception was thrown, but no records were updated,either. // Might want to let the user know that the update failed } }