SqlDataSource.Insert 方法

定义

使用 InsertCommand SQL 字符串和 InsertParameters 集合中的所有参数执行插入操作。

public:
 int Insert();
public int Insert ();
member this.Insert : unit -> int
Public Function Insert () As Integer

返回

一个值,该值表示插入到基础数据库中的行数。

例外

SqlDataSource 无法与基础数据源建立连接。

示例

下面的代码示例演示如何使用 SqlDataSource 控件和简单的Web Forms页将数据插入数据库。 Data 表中的当前数据显示在 控件中 DropDownList 。 可以通过在控件中 TextBox 输入值,然后单击“ 插入 ”按钮来添加新记录。 单击“ 插入 ”按钮时,会将指定的值插入数据库,然后 DropDownList 刷新 。

重要

此示例包含一个文本框,该文本框接受用户输入,这是一种潜在的安全威胁,并且值插入到参数中而不进行验证,这也是一种潜在的安全威胁。 在执行查询之前, Inserting 使用 事件验证参数值。 有关详细信息,请参阅脚本侵入概述

注意

此示例演示如何使用声明性语法进行数据访问。 有关如何使用代码而不是标记访问数据的信息,请参阅 在 Visual Studio 中访问数据

<%@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">
private void InsertShipper (object source, EventArgs e) {
  SqlDataSource1.Insert();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <asp:dropdownlist
        id="DropDownList1"
        runat="server"
        datasourceid="SqlDataSource1"
        datatextfield="CompanyName"
        datavaluefield="ShipperID" />

<!-- Security Note: The SqlDataSource uses a FormParameter,
     Security Note: which does not perform validation of input from the client.
     Security Note: To validate the value of the FormParameter, handle the Inserting event. -->

      <asp:sqldatasource
        id="SqlDataSource1"
        runat="server"
        connectionstring="<%$ ConnectionStrings:MyNorthwind %>"
        selectcommand="SELECT CompanyName,ShipperID FROM Shippers"
        insertcommand="INSERT INTO Shippers (CompanyName,Phone) VALUES (@CoName,@Phone)">
          <insertparameters>
            <asp:formparameter name="CoName" formfield="CompanyNameBox" />
            <asp:formparameter name="Phone"  formfield="PhoneBox" />
          </insertparameters>
      </asp:sqldatasource>

      <br /><asp:textbox
           id="CompanyNameBox"
           runat="server" />

      <asp:RequiredFieldValidator
        id="RequiredFieldValidator1"
        runat="server"
        ControlToValidate="CompanyNameBox"
        Display="Static"
        ErrorMessage="Please enter a company name." />

      <br /><asp:textbox
           id="PhoneBox"
           runat="server" />

      <asp:RequiredFieldValidator
        id="RequiredFieldValidator2"
        runat="server"
        ControlToValidate="PhoneBox"
        Display="Static"
        ErrorMessage="Please enter a phone number." />

      <br /><asp:button
           id="Button1"
           runat="server"
           text="Insert New Shipper"
           onclick="InsertShipper" />

    </form>
  </body>
</html>
<%@Page  Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
Private Sub InsertShipper (ByVal Source As Object, ByVal e As EventArgs)
  SqlDataSource1.Insert()
End Sub ' InsertShipper
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <asp:dropdownlist
        id="DropDownList1"
        runat="server"
        datasourceid="SqlDataSource1"
        datatextfield="CompanyName"
        datavaluefield="ShipperID" />

<!-- Security Note: The SqlDataSource uses a FormParameter,
     Security Note: which does not perform validation of input from the client.
     Security Note: To validate the value of the FormParameter, handle the Inserting event. -->

      <asp:sqldatasource
        id="SqlDataSource1"
        runat="server"
        connectionstring="<%$ ConnectionStrings:MyNorthwind %>"
        selectcommand="SELECT CompanyName,ShipperID FROM Shippers"
        insertcommand="INSERT INTO Shippers (CompanyName,Phone) VALUES (@CoName,@Phone)">
          <insertparameters>
            <asp:formparameter name="CoName" formfield="CompanyNameBox" />
            <asp:formparameter name="Phone"  formfield="PhoneBox" />
          </insertparameters>
      </asp:sqldatasource>

      <br /><asp:textbox
           id="CompanyNameBox"
           runat="server" />

      <asp:RequiredFieldValidator
        id="RequiredFieldValidator1"
        runat="server"
        ControlToValidate="CompanyNameBox"
        Display="Static"
        ErrorMessage="Please enter a company name." />

      <br /><asp:textbox
           id="PhoneBox"
           runat="server" />

      <asp:RequiredFieldValidator
        id="RequiredFieldValidator2"
        runat="server"
        ControlToValidate="PhoneBox"
        Display="Static"
        ErrorMessage="Please enter a phone number." />

      <br /><asp:button
           id="Button1"
           runat="server"
           text="Insert New Shipper"
           onclick="InsertShipper" />

    </form>
  </body>
</html>

注解

在执行插入操作之前, OnInserting 将调用 方法来引发 Inserting 事件。 可以处理此事件以检查参数的值,并在操作之前 Insert 执行任何预处理。 为了执行插入操作,SqlDataSourceView对象使用InsertCommand文本和任何关联的InsertParameters属性生成对象DbCommand,然后针对基础数据库执行DbCommand对象。

操作完成后, OnInserted 将调用 方法来引发 Inserted 事件。 可以处理此事件以检查任何返回值和错误代码,并执行任何后期处理。

方法 Insert 用于以编程方式访问 Insert 方法。 如果控件 SqlDataSource 与数据绑定控件相关联,则数据绑定控件会自动调用 Insert 方法。

方法Insert委托给InsertSqlDataSource 控件关联的 对象的 方法SqlDataSourceView

重要

值在没有验证的情况下插入到参数中,这是一种潜在的安全威胁。 在执行查询之前, Filtering 使用 事件验证参数值。 有关详细信息,请参阅脚本侵入概述

适用于

另请参阅