SqlDataSourceView.Update(IDictionary, IDictionary, IDictionary) Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Effectue une opération de mise à jour à l’aide de la UpdateCommand chaîne SQL, de tous les paramètres qui se trouvent dans la UpdateParameters collection et des valeurs qui se trouvent dans les collections et les collections spécifiésvalueskeysoldValues.
public:
int Update(System::Collections::IDictionary ^ keys, System::Collections::IDictionary ^ values, System::Collections::IDictionary ^ oldValues);
public int Update(System.Collections.IDictionary keys, System.Collections.IDictionary values, System.Collections.IDictionary oldValues);
override this.Update : System.Collections.IDictionary * System.Collections.IDictionary * System.Collections.IDictionary -> int
Public Function Update (keys As IDictionary, values As IDictionary, oldValues As IDictionary) As Integer
Paramètres
- keys
- IDictionary
Clé IDictionary primaire à utiliser avec la UpdateCommand propriété pour effectuer l’opération de mise à jour de la base de données. S’il n’y a pas de clés associées à la requête ou s’il UpdateCommand ne s’agit pas d’une requête SQL paramétrée, passez null.
- values
- IDictionary
Valeurs IDictionary à utiliser avec la UpdateCommand propriété pour effectuer l’opération de mise à jour de la base de données. S’il n’existe aucun paramètre associé à la requête ou s’il UpdateCommand ne s’agit pas d’une requête SQL paramétrable, transmettez null.
- oldValues
- IDictionary
Qui IDictionary représente les valeurs d’origine dans la base de données. S’il n’existe aucun paramètre associé à la requête ou s’il UpdateCommand ne s’agit pas d’une requête SQL paramétrable, transmettez null.
Retours
Valeur qui représente le nombre de lignes mises à jour dans la base de données sous-jacente.
Exceptions
Impossible SqlDataSource d’établir une connexion avec la source de données sous-jacente.
La CanUpdate propriété est false.
Exemples
L’exemple de code suivant montre comment utiliser un SqlDataSource contrôle pour afficher des données dans un DropDownList contrôle et mettre à jour les données lorsque le bouton Envoyer est cliqué. La UpdateCommand propriété est définie avec une instruction SQL paramétrable et deux ControlParameter paramètres sont ajoutés à la UpdateParameters collection. Lorsque le bouton Envoyer est cliqué, l’événement OnClick est géré pour appeler la Update méthode explicitement.
<%@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>
Remarques
La Update méthode appelle la ExecuteUpdate méthode, en passant les paramètres et oldValues les keysvaluesparamètres. Pour plus d’informations, consultez SqlDataSource.UpdateCommand.