sqlDataSource 动态查询

2024-01-15 03:58
文章标签 动态 查询 sqldatasource

本文主要是介绍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>

这篇关于sqlDataSource 动态查询的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/607572

相关文章

慢sql提前分析预警和动态sql替换-Mybatis-SQL

《慢sql提前分析预警和动态sql替换-Mybatis-SQL》为防止慢SQL问题而开发的MyBatis组件,该组件能够在开发、测试阶段自动分析SQL语句,并在出现慢SQL问题时通过Ducc配置实现动... 目录背景解决思路开源方案调研设计方案详细设计使用方法1、引入依赖jar包2、配置组件XML3、核心配

MyBatis模糊查询报错:ParserException: not supported.pos 问题解决

《MyBatis模糊查询报错:ParserException:notsupported.pos问题解决》本文主要介绍了MyBatis模糊查询报错:ParserException:notsuppo... 目录问题描述问题根源错误SQL解析逻辑深层原因分析三种解决方案方案一:使用CONCAT函数(推荐)方案二:

springboot使用Scheduling实现动态增删启停定时任务教程

《springboot使用Scheduling实现动态增删启停定时任务教程》:本文主要介绍springboot使用Scheduling实现动态增删启停定时任务教程,具有很好的参考价值,希望对大家有... 目录1、配置定时任务需要的线程池2、创建ScheduledFuture的包装类3、注册定时任务,增加、删

MySQL 中的 JSON 查询案例详解

《MySQL中的JSON查询案例详解》:本文主要介绍MySQL的JSON查询的相关知识,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录mysql 的 jsON 路径格式基本结构路径组件详解特殊语法元素实际示例简单路径复杂路径简写操作符注意MySQL 的 J

Go语言开发实现查询IP信息的MCP服务器

《Go语言开发实现查询IP信息的MCP服务器》随着MCP的快速普及和广泛应用,MCP服务器也层出不穷,本文将详细介绍如何在Go语言中使用go-mcp库来开发一个查询IP信息的MCP... 目录前言mcp-ip-geo 服务器目录结构说明查询 IP 信息功能实现工具实现工具管理查询单个 IP 信息工具的实现服

SpringBoot基于配置实现短信服务策略的动态切换

《SpringBoot基于配置实现短信服务策略的动态切换》这篇文章主要为大家详细介绍了SpringBoot在接入多个短信服务商(如阿里云、腾讯云、华为云)后,如何根据配置或环境切换使用不同的服务商,需... 目录目标功能示例配置(application.yml)配置类绑定短信发送策略接口示例:阿里云 & 腾

MySQL索引的优化之LIKE模糊查询功能实现

《MySQL索引的优化之LIKE模糊查询功能实现》:本文主要介绍MySQL索引的优化之LIKE模糊查询功能实现,本文通过示例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录一、前缀匹配优化二、后缀匹配优化三、中间匹配优化四、覆盖索引优化五、减少查询范围六、避免通配符开头七、使用外部搜索引擎八、分

SQL表间关联查询实例详解

《SQL表间关联查询实例详解》本文主要讲解SQL语句中常用的表间关联查询方式,包括:左连接(leftjoin)、右连接(rightjoin)、全连接(fulljoin)、内连接(innerjoin)、... 目录简介样例准备左外连接右外连接全外连接内连接交叉连接自然连接简介本文主要讲解SQL语句中常用的表

MySQL高级查询之JOIN、子查询、窗口函数实际案例

《MySQL高级查询之JOIN、子查询、窗口函数实际案例》:本文主要介绍MySQL高级查询之JOIN、子查询、窗口函数实际案例的相关资料,JOIN用于多表关联查询,子查询用于数据筛选和过滤,窗口函... 目录前言1. JOIN(连接查询)1.1 内连接(INNER JOIN)1.2 左连接(LEFT JOI

MySQL 中查询 VARCHAR 类型 JSON 数据的问题记录

《MySQL中查询VARCHAR类型JSON数据的问题记录》在数据库设计中,有时我们会将JSON数据存储在VARCHAR或TEXT类型字段中,本文将详细介绍如何在MySQL中有效查询存储为V... 目录一、问题背景二、mysql jsON 函数2.1 常用 JSON 函数三、查询示例3.1 基本查询3.2