Asp.net 2.0 SqlDataSource的高级应用

本文介绍 ASP.NET 中 SqlDataSource 控件的高级应用技巧,包括如何为 Select 和 Update 操作设置不同的参数,以及如何通过编程方式动态绑定参数值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SqlDataSource主要用于SqlServer 数据库,Access数据库。

ObjectDataSource则用于对象数据模型。比如自定义的可提供数据的类。

二者配合基本上可实现常规的所有功能。

现在谈一下,数据源控件的高级应用之为SqlDataSource绑定与Select不同参数的UpdateDeleteInsert相关的参数

 可用提供的六大参数加默认参数法在页面上进行绑定。

 可在后面写纯代码绑定。

 可二者结合:即相关查询语句写关页面上,参数的值可以由某个程序逻辑运算后再进行绑定赋值。

由于时间关系,看每三种,前面两是三个基础看例子:

 

 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ExternalUpdateRow.aspx.cs" Inherits="ExternalUpdateRow" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

        <table>

            <tr>

                <td colspan="4">

        <asp:GridView ID="gviewEmployee" runat="server" AutoGenerateColumns="False" DataSourceID="sqlDS" OnSorted="gviewEmployee_Sorted" OnPageIndexChanging="gviewEmployee_PageIndexChanging" OnSelectedIndexChanging="gviewEmployee_SelectedIndexChanging">

        </asp:GridView>

      <asp:SqlDataSource ID="sqlDS" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" OnUpdated="sqlDS_Updated" OnUpdating="sqlDS_Updating" SelectCommand="select EmployeeID,LastName,City,Address from Employees Where City=@City"  UpdateCommand="Update Employees set LastName=@txtLastName,City=@txtCity where EmployeeID=@txtEmployeeID" ProviderName="<%$ ConnectionStrings:NorthwindConnectionString.ProviderName %>">

      <SelectParameters>

      <asp:Parameter Name="City" Type="String" DefaultValue="Seattle" />

          </SelectParameters>

      </asp:SqlDataSource>

                    &nbsp;

                </td>

            </tr>

            <tr>

                <td style="width: 156px">

                    <asp:Label ID="txtLastNameTitle" runat="server" Text="姓名:"></asp:Label>

                    <asp:TextBox ID="txtLastName" runat="server" Width="80px"></asp:TextBox></td>

                <td style="width: 155px">

                    <asp:Label ID="txtCityTitle" runat="server" Text="县市:"></asp:Label>

                    <asp:TextBox ID="txtCity" runat="server" Width="80px"></asp:TextBox></td>

                <td style="width: 223px" colspan="2">

                    <asp:Button ID="btnUpdate" runat="server" Text="更新数据" OnClick="btnUpdate_Click" />&nbsp;

                    <asp:Button ID="btnCancel" runat="server" OnClick="btnCancel_Click" Text="取消" Width="71px" /></td>

            </tr>

        </table>

        <br />

    <div>

        &nbsp;</div>

    </form>

</body>

</html>

即:

 我们在页面上写了Select 和 Update语句。

SelectCommand="select EmployeeID,LastName,City,Address from Employees Where City=@City" 

UpdateCommand="Update Employees set LastName=@txtLastName,City=@txtCity where EmployeeID=@txtEmployeeID" ProviderName="<%$ ConnectionStrings:NorthwindConnectionString.ProviderName %>">

注意的是,他们的参数不一样,前些天用数据源控件配置不同参数时(即Select 语句和其他类型语句参数不一们),发现用控件直接配置竟不行,很纳闷。经过一番试验,发现了上面的三条规律,有不合理的地方。望指正。

说正题:

点击更新句:

 

核心代码:

 

 

sqlDS.UpdateParameters.Add("txtEmployeeID", TypeCode.Int32, gviewEmployee.Rows[gviewEmployee.SelectedIndex].Cells[1].Text);

        sqlDS.UpdateParameters.Add("txtLastName", TypeCode.String, txtLastName.Text);

        sqlDS.UpdateParameters.Add("txtCity", TypeCode.String, txtCity.Text);

     

       ……

 

说明:txtEmployeeIDSqlServer参数名,这里没有 @ ,估计是微软为了让新手能快速上手而让人不去管Sql语句与asp.net参数的不同。不说了,看结果。

运行:

  

选择“1”行:并更改数据 DavolioDavolio2

 

改变了哈。说明程序能运行。即上述所说的第三种情况。

联想:

  我们可以任意的调用一个数据集后(可以是存储过程产生的数据集),然后用调用任意的(是数据库中的另一张表),进行操作。而不会导致像配置那样,不能对参数进行配置,而只能在数据源控件上配置与Select语参数相同的参数。这样大增强了控件应用与自定义程序的灵活性。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值