SqlDataSource.Insert 方法

定義

使用 InsertCommand SQL 字串和 InsertParameters 集合中的任何參數,執行插入作業。

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

傳回

表示插入基礎資料庫之資料列數的值。

例外狀況

SqlDataSource 不能以基礎資料來源建立連接。

範例

下列程式碼範例示範如何使用 控制項和簡單的Web Form頁面將資料插入資料庫中 SqlDataSource 。 資料表中的目前資料會顯示在 控制項中 DropDownList 。 您可以在控制項中 TextBox 輸入值,然後按一下 [ 插入 ] 按鈕,以新增記錄。 按一下 [ 插入 ] 按鈕時,指定的值會插入資料庫中,然後 DropDownList 重新整理 。

重要

此範例包含一個文字方塊,可接受使用者輸入,這是潛在的安全性威脅,且值會插入參數中而不進行驗證,這也是潛在的安全性威脅。 在執行查詢之前, Inserting 請使用 事件來驗證參數值。 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。

注意

此範例示範如何使用宣告式語法進行資料存取。 如需如何使用程式碼而非標記來存取資料的詳細資訊,請參閱 在 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 請使用 事件來驗證參數值。 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。

適用於

另請參閱