本文主要是介绍sqlDataSource 动态查询,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
<asp:sqlDataSource ID="EmployeeDetailsSqlDataSource" SelectCommand="SELEC T EmployeeID, LastName, FirstName FROM Employees WHER E (EmployeeName = @EmpName or @EmpName is null) " 这样,@EmpName没有赋值就等于不要这个条件来查询了啊 aspx: <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1"> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ChemSearchConnectionString %>" SelectCommand='<%# GetSelectCommandText() %>' > <%--动态绑定 SelectCommand 命令--%> </asp:SqlDataSource> cs: protected void Page_Load(object sender, EventArgs e) { SqlDataSource1.DataBind(); } protected string GetSelectCommandText() { string sql = "SELECT TOP 5 * FROM [CSInfo]"; return sql; } 或者在aspx中不写SelectCommand 直接在后台写 SqlDataSource1.SelectCommand = "SELECT TOP 5 * FROM [CSInfo]"; |
对于 xxxDataSource 来说,支持绑定参数,包括 ControlParameter、CookieParameter、SessionParameter、ProfileParameter 和 QueryStringParameter。假如参数值直接来自于应用程序变量或者通过某个方法返回呢?
查阅了关于参数基类 Parameter 类 似乎不支持此功能,有一个选择就是扩展自己的 Parameter,但是工作量比大,本身使用 xxxDataSource 就是为了快速开发。
这里采用比较“原始”方法:直接使用Web服务器控件都支持的绑定语法 <%# expression %>
先看下面这个 SqlDataSource ,其中的 SelectCommand 属性,是通过动态绑定实现的,categoryId 是一个私有类字段。
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=.;Initial Catalog=Northwind;Integrated Security=True"
ProviderName="System.Data.SqlClient" SelectCommand='<%# "SELECT * FROM [Products] WHERE [CategoryID] = " + categoryId %>'> <%--动态绑定 SelectCommand 命令--%>
</asp:SqlDataSource>
可以通过控件事件中改变类字段 categoryId 的值,然后调用 SqlDataSource1.DataBind() 计算此值,得出 SelectCommand
甚至可以绑定一个方法,处理一个比较复杂sql语句,并返回
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="Data Source=.;Initial Catalog=Northwind;Integrated Security=True"
ProviderName="System.Data.SqlClient" SelectCommand='<%# GetSelectCommandText() %>' > <%--动态绑定 SelectCommand 命令--%>
</asp:SqlDataSource>
private string GetSelectCommandText()
{
string sql = "SELECT * FROM [Products]";
if (DropDownList1.SelectedValue != "") {
sql += " WHERE [CategoryID] = " + int.Parse(DropDownList1.SelectedValue);
}
return sql;
}
测试实例通过一个 DropDownList 改变 categoryId 的值
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
categoryId = int.Parse(DropDownList1.SelectedValue);
SqlDataSource1.DataBind(); // 先执行绑定数据源控件,计算 SelectCommand
GridView1.DataBind();
SqlDataSource3.DataBind(); // 先执行绑定数据源控件,计算 SelectCommand
GridView2.DataBind();
}
其实,都是 ASP.NET 1.x 中数据绑定的应用而已,唯一需要注意的是,调用数据控件(如GridView)的 DataBind 方法之前一定要先调用数据源控件(如SqlDataSource)的 DataBind() 方法。
完整代码:
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected int categoryId = 1;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack) {
SqlDataSource1.DataBind();
SqlDataSource3.DataBind();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
categoryId = int.Parse(DropDownList1.SelectedValue);
SqlDataSource1.DataBind(); // 先执行绑定数据源控件,计算 SelectCommand
GridView1.DataBind();
SqlDataSource3.DataBind(); // 先执行绑定数据源控件,计算 SelectCommand
GridView2.DataBind();
}
private string GetSelectCommandText()
{
string sql = "SELECT * FROM [Products]";
if (DropDownList1.SelectedValue != "") {
sql += " WHERE [CategoryID] = " + int.Parse(DropDownList1.SelectedValue);
}
return sql;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>DataBindForSelectCommand2</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2" AutoPostBack="true"
DataTextField="CategoryName" DataValueField="CategoryID" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="Data Source=.;Initial Catalog=Northwind;Integrated Security=True"
ProviderName="System.Data.SqlClient" SelectCommand="SELECT [CategoryID], [CategoryName] FROM [Categories]">
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID"
DataSourceID="SqlDataSource1" >
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="ProductID" InsertVisible="False"
ReadOnly="True" SortExpression="ProductID" />
<asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=.;Initial Catalog=Northwind;Integrated Security=True"
ProviderName="System.Data.SqlClient" SelectCommand='<%# "SELECT * FROM [Products] WHERE [CategoryID] = " + categoryId %>'> <%--动态绑定 SelectCommand 命令--%>
</asp:SqlDataSource>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID"
DataSourceID="SqlDataSource3" >
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="ProductID" InsertVisible="False"
ReadOnly="True" SortExpression="ProductID" />
<asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="Data Source=.;Initial Catalog=Northwind;Integrated Security=True"
ProviderName="System.Data.SqlClient" SelectCommand='<%# GetSelectCommandText() %>' > <%--动态绑定 SelectCommand 命令--%>
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
查阅了关于参数基类 Parameter 类 似乎不支持此功能,有一个选择就是扩展自己的 Parameter,但是工作量比大,本身使用 xxxDataSource 就是为了快速开发。
这里采用比较“原始”方法:直接使用Web服务器控件都支持的绑定语法 <%# expression %>
先看下面这个 SqlDataSource ,其中的 SelectCommand 属性,是通过动态绑定实现的,categoryId 是一个私有类字段。
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=.;Initial Catalog=Northwind;Integrated Security=True"
ProviderName="System.Data.SqlClient" SelectCommand='<%# "SELECT * FROM [Products] WHERE [CategoryID] = " + categoryId %>'> <%--动态绑定 SelectCommand 命令--%>
</asp:SqlDataSource>
可以通过控件事件中改变类字段 categoryId 的值,然后调用 SqlDataSource1.DataBind() 计算此值,得出 SelectCommand
甚至可以绑定一个方法,处理一个比较复杂sql语句,并返回
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="Data Source=.;Initial Catalog=Northwind;Integrated Security=True"
ProviderName="System.Data.SqlClient" SelectCommand='<%# GetSelectCommandText() %>' > <%--动态绑定 SelectCommand 命令--%>
</asp:SqlDataSource>
private string GetSelectCommandText()
{
string sql = "SELECT * FROM [Products]";
if (DropDownList1.SelectedValue != "") {
sql += " WHERE [CategoryID] = " + int.Parse(DropDownList1.SelectedValue);
}
return sql;
}
测试实例通过一个 DropDownList 改变 categoryId 的值
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
categoryId = int.Parse(DropDownList1.SelectedValue);
SqlDataSource1.DataBind(); // 先执行绑定数据源控件,计算 SelectCommand
GridView1.DataBind();
SqlDataSource3.DataBind(); // 先执行绑定数据源控件,计算 SelectCommand
GridView2.DataBind();
}
其实,都是 ASP.NET 1.x 中数据绑定的应用而已,唯一需要注意的是,调用数据控件(如GridView)的 DataBind 方法之前一定要先调用数据源控件(如SqlDataSource)的 DataBind() 方法。
完整代码:
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected int categoryId = 1;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack) {
SqlDataSource1.DataBind();
SqlDataSource3.DataBind();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
categoryId = int.Parse(DropDownList1.SelectedValue);
SqlDataSource1.DataBind(); // 先执行绑定数据源控件,计算 SelectCommand
GridView1.DataBind();
SqlDataSource3.DataBind(); // 先执行绑定数据源控件,计算 SelectCommand
GridView2.DataBind();
}
private string GetSelectCommandText()
{
string sql = "SELECT * FROM [Products]";
if (DropDownList1.SelectedValue != "") {
sql += " WHERE [CategoryID] = " + int.Parse(DropDownList1.SelectedValue);
}
return sql;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>DataBindForSelectCommand2</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2" AutoPostBack="true"
DataTextField="CategoryName" DataValueField="CategoryID" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="Data Source=.;Initial Catalog=Northwind;Integrated Security=True"
ProviderName="System.Data.SqlClient" SelectCommand="SELECT [CategoryID], [CategoryName] FROM [Categories]">
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID"
DataSourceID="SqlDataSource1" >
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="ProductID" InsertVisible="False"
ReadOnly="True" SortExpression="ProductID" />
<asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=.;Initial Catalog=Northwind;Integrated Security=True"
ProviderName="System.Data.SqlClient" SelectCommand='<%# "SELECT * FROM [Products] WHERE [CategoryID] = " + categoryId %>'> <%--动态绑定 SelectCommand 命令--%>
</asp:SqlDataSource>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID"
DataSourceID="SqlDataSource3" >
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="ProductID" InsertVisible="False"
ReadOnly="True" SortExpression="ProductID" />
<asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="Data Source=.;Initial Catalog=Northwind;Integrated Security=True"
ProviderName="System.Data.SqlClient" SelectCommand='<%# GetSelectCommandText() %>' > <%--动态绑定 SelectCommand 命令--%>
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
这篇关于sqlDataSource 动态查询的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!