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 페이지입니다. 데이터 테이블의 현재 데이터에 표시 됩니다는 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 빌드 개체는 DbCommand 를 사용 하 여 개체를 InsertCommand 텍스트 및 연결 된 InsertParameters 속성을 다음 실행는 DbCommand 기본 데이터베이스에 대 한 개체입니다.

작업이 완료 되 면 합니다 OnInserted 메서드를 호출 발생 하는 Inserted 이벤트. 모든 반환 값 및 오류 코드를 검사 하 고 사후 처리를 수행 하려면이 이벤트를 처리할 수 있습니다.

합니다 Insert 메서드를 프로그래밍 방식 액세스를 위해 제공 됩니다는 Insert 메서드. 경우는 SqlDataSource 컨트롤은 데이터 바인딩된 컨트롤을 사용 하 여 연결, 데이터 바인딩된 컨트롤을 자동으로 호출 된 Insert 메서드.

Insert 메서드를 Insert 메서드를 SqlDataSourceView 연관 된 개체는 SqlDataSource 컨트롤.

중요

값 매개 변수 유효성 검사는 잠재적인 보안 위협을 없이 삽입 됩니다. 사용 된 Filtering 이벤트 쿼리를 실행 하기 전에 매개 변수 값의 유효성 검사를 합니다. 자세한 내용은 Script Exploits Overview를 참조하세요.

적용 대상

추가 정보