SqlDataSource.Update 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
SQL 문자열 및 컬렉션에 UpdateCommand 있는 UpdateParameters 모든 매개 변수를 사용하여 업데이트 작업을 수행합니다.
public:
int Update();
public int Update();
member this.Update : unit -> int
Public Function Update () As Integer
반품
기본 데이터베이스에서 업데이트된 행 수를 나타내는 값입니다.
예외
SqlDataSource 기본 데이터 원본과의 연결을 설정할 수 없습니다.
예제
이 섹션에는 두 가지 코드 예제가 포함되어 있습니다. 첫 번째 코드 예제에서는 컨트롤을 SqlDataSource 사용하여 컨트롤에 DropDownList 데이터를 표시하고 전송 단추를 클릭할 때 데이터를 업데이트하는 방법을 보여 줍니다. 두 번째 코드 예제에서는 컨트롤의 Microsoft SQL Server 데이터베이스 DropDownList 에서 검색된 데이터를 표시하고 컨트롤을 사용하여 TextBox 레코드를 업데이트하는 방법을 보여 줍니다.
다음 코드 예제에서는 컨트롤을 사용 하 여 SqlDataSource 컨트롤에 DropDownList 데이터를 표시 하 고 전송 단추를 클릭할 때 데이터를 업데이트 하는 방법을 보여 줍니다. 속성은 UpdateCommand 매개 변수가 있는 SQL 문으로 설정되고 두 개의 ControlParameter 매개 변수가 컬렉션에 UpdateParameters 추가됩니다. 제출 단추를 클릭하면 메서드를 OnClick 명시적으로 호출 Update 하기 위해 이벤트가 처리됩니다.
<%@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 On_Click(Object source, EventArgs e) {
try {
SqlDataSource1.Update();
}
catch (Exception except) {
// Handle the Exception.
}
Label2.Text="The record was updated successfully!";
}
</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:SqlDataSource
id="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
SelectCommand="SELECT EmployeeID, LastName, Address FROM Employees"
UpdateCommand="UPDATE Employees SET Address=@Address WHERE EmployeeID=@EmployeeID">
<UpdateParameters>
<asp:ControlParameter Name="Address" ControlId="TextBox1" PropertyName="Text"/>
<asp:ControlParameter Name="EmployeeID" ControlId="DropDownList1" PropertyName="SelectedValue"/>
</UpdateParameters>
</asp:SqlDataSource>
<asp:DropDownList
id="DropDownList1"
runat="server"
DataTextField="LastName"
DataValueField="EmployeeID"
DataSourceID="SqlDataSource1">
</asp:DropDownList>
<br />
<asp:Label id="Label1" runat="server" Text="Enter a new address for the selected user."
AssociatedControlID="TextBox1" />
<asp:TextBox id="TextBox1" runat="server" />
<asp:Button id="Submit" runat="server" Text="Submit" OnClick="On_Click" />
<br /><asp:Label id="Label2" runat="server" Text="" />
</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">
Sub On_Click(ByVal source As Object, ByVal e As EventArgs)
Try
SqlDataSource1.Update()
Catch except As Exception
' Handle the Exception.
End Try
Label2.Text="The record was updated successfully!"
End Sub 'On_Click
</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:SqlDataSource
id="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
SelectCommand="SELECT EmployeeID, LastName, Address FROM Employees"
UpdateCommand="UPDATE Employees SET Address=@Address WHERE EmployeeID=@EmployeeID">
<UpdateParameters>
<asp:ControlParameter Name="Address" ControlId="TextBox1" PropertyName="Text"/>
<asp:ControlParameter Name="EmployeeID" ControlId="DropDownList1" PropertyName="SelectedValue"/>
</UpdateParameters>
</asp:SqlDataSource>
<asp:DropDownList
id="DropDownList1"
runat="server"
DataTextField="LastName"
DataValueField="EmployeeID"
DataSourceID="SqlDataSource1">
</asp:DropDownList>
<br />
<asp:Label id="Label1" runat="server" Text="Enter a new address for the selected user."
AssociatedControlID="TextBox1" />
<asp:TextBox id="TextBox1" runat="server" />
<asp:Button id="Submit" runat="server" Text="Submit" OnClick="On_Click" />
<br /><asp:Label id="Label2" runat="server" Text="" />
</form>
</body>
</html>
다음 코드 예제에서는 컨트롤의 SQL Server 데이터베이스 DropDownList 에서 검색된 데이터를 표시하고 컨트롤을 사용하여 TextBox 레코드를 업데이트하는 방법을 보여 줍니다. 이 예제에서는 컨트롤을 DbTransaction 사용하여 데이터를 업데이트할 때 개체를 사용하여 트랜잭션 컨텍스트를 SqlDataSource 추가하는 방법을 보여줍니다.
<%@Page Language="C#" %>
<%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Data.Common" %>
<%@Import Namespace="System.Data.SqlClient" %>
<%@Import Namespace="System.Diagnostics" %>
<!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 On_Click(Object source, EventArgs e) {
SqlDataSource1.Update();
}
private void OnSqlUpdating(Object source, SqlDataSourceCommandEventArgs e) {
DbCommand command = e.Command;
DbConnection cx = command.Connection;
cx.Open();
DbTransaction tx = cx.BeginTransaction();
command.Transaction = tx;
}
private void OnSqlUpdated(Object source, SqlDataSourceStatusEventArgs e) {
DbCommand command = e.Command;
DbTransaction tx = command.Transaction;
// In this code example the OtherProcessSucceeded variable represents
// the outcome of some other process that occurs whenever the data is
// updated, and must succeed for the data change to be committed. For
// simplicity, we set this value to true.
bool OtherProcessSucceeded = true;
if (OtherProcessSucceeded) {
tx.Commit();
Label2.Text="The record was updated successfully!";
}
else {
tx.Rollback();
Label2.Text="The record was not updated.";
}
}
</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:SqlDataSource
id="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
SelectCommand="SELECT EmployeeID, LastName, Address FROM Employees"
UpdateCommand="UPDATE Employees SET Address=@Address WHERE EmployeeID=@EmployeeID"
OnUpdating="OnSqlUpdating"
OnUpdated ="OnSqlUpdated">
<UpdateParameters>
<asp:ControlParameter Name="Address" ControlId="TextBox1" PropertyName="Text"/>
<asp:ControlParameter Name="EmployeeID" ControlId="DropDownList1" PropertyName="SelectedValue"/>
</UpdateParameters>
</asp:SqlDataSource>
<asp:DropDownList
id="DropDownList1"
runat="server"
DataTextField="LastName"
DataValueField="EmployeeID"
DataSourceID="SqlDataSource1">
</asp:DropDownList>
<br />
<asp:Label id="Label1" runat="server" Text="Enter a new address for the selected user."
AssociatedControlID="TextBox1" />
<asp:TextBox id="TextBox1" runat="server" />
<asp:Button id="Submit" runat="server" Text="Submit" OnClick="On_Click" />
<br /><asp:Label id="Label2" runat="server" Text="" />
</form>
</body>
</html>
<%@Page Language="VB" %>
<%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Data.Common" %>
<%@Import Namespace="System.Diagnostics" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Sub On_Click(ByVal source As Object, ByVal e As EventArgs)
SqlDataSource1.Update()
End Sub 'On_Click
Sub On_Sql_Updating(ByVal source As Object, ByVal e As SqlDataSourceCommandEventArgs)
Dim command as DbCommand
Dim connection as DbConnection
Dim transaction as DbTransaction
command = e.Command
connection = command.Connection
connection.Open()
transaction = connection.BeginTransaction()
command.Transaction = transaction
End Sub 'On_Sql_Updating
Sub On_Sql_Updated(ByVal source As Object, ByVal e As SqlDataSourceStatusEventArgs)
Dim command As DbCommand
Dim transaction As DbTransaction
command = e.Command
transaction = command.Transaction
' In this code example the OtherProcessSucceeded variable represents
' the outcome of some other process that occurs whenever the data is
' updated, and must succeed for the data change to be committed. For
' simplicity, we set this value to true.
Dim OtherProcessSucceeded as Boolean = True
If (OtherProcessSucceeded) Then
transaction.Commit()
Label2.Text="The record was updated successfully!"
Else
transaction.Rollback()
Label2.Text="The record was not updated."
End If
End Sub ' On_Sql_Updated
</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:SqlDataSource
id="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
SelectCommand="SELECT EmployeeID, LastName, Address FROM Employees"
UpdateCommand="UPDATE Employees SET Address=@Address WHERE EmployeeID=@EmployeeID"
OnUpdating="On_Sql_Updating"
OnUpdated ="On_Sql_Updated">
<UpdateParameters>
<asp:ControlParameter Name="Address" ControlId="TextBox1" PropertyName="Text"/>
<asp:ControlParameter Name="EmployeeID" ControlId="DropDownList1" PropertyName="SelectedValue"/>
</UpdateParameters>
</asp:SqlDataSource>
<asp:DropDownList
id="DropDownList1"
runat="server"
DataTextField="LastName"
DataValueField="EmployeeID"
DataSourceID="SqlDataSource1">
</asp:DropDownList>
<br />
<asp:Label id="Label1" runat="server" Text="Enter a new address for the selected user."
AssociatedControlID="TextBox1" />
<asp:TextBox id="TextBox1" runat="server" />
<asp:Button id="Submit" runat="server" Text="Submit" OnClick="On_Click" />
<br /><asp:Label id="Label2" runat="server" Text="" />
</form>
</body>
</html>
설명
데이터가 변경된 경우 메서드가 Update 자동으로 호출GridViewDetailsView되고FormView, 포스트백 중에 컨트롤이 호출됩니다. 다른 컨트롤 Update 에서 변경된 데이터의 경우 이벤트 중에 Load 포스트백 시 메서드를 명시적으로 호출할 수 있습니다.
Update 작업을 수행하기 OnUpdating 전에 이벤트를 발생 하려면 메서드를 호출 합니다Updating. 이 이벤트를 처리하여 매개 변수 값을 검사하고 작업 전에 Update 전처리를 수행할 수 있습니다.
Update 작업이 완료되면 OnUpdated 이벤트를 발생하도록 메서드가 Updated 호출됩니다. 이 이벤트를 처리하여 반환 값 및 오류 코드를 검사하고 사후 처리를 수행할 수 있습니다.
메서드는 Update 컨트롤과 Update 연결된 개체의 SqlDataSourceView 메서드에 대리합니다 SqlDataSource . 업데이트 작업을 SqlDataSourceView 수행하려면 텍스트 및 연결된 속성을 사용하여 UpdateCommand 개체를 빌드 DbCommand 한 UpdateParameters 다음 기본 데이터베이스에 대해 개체를 실행합니다DbCommand.
중요합니다
값은 유효성 검사 없이 매개 변수에 삽입되며 이는 잠재적인 보안 위협입니다. 쿼리를 Updating 실행하기 전에 이벤트를 사용하여 매개 변수 값의 유효성을 검사합니다. 자세한 내용은 스크립트 악용 개요를 참조하세요.