SqlDataSourceView.Update(IDictionary, IDictionary, IDictionary) Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Esegue un'operazione di aggiornamento usando la UpdateCommand stringa SQL, tutti i parametri presenti nella UpdateParameters raccolta e i valori presenti nelle raccolte , valuese oldValues specificatikeys.
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
Parametri
- keys
- IDictionary
Oggetto IDictionary delle chiavi primarie da utilizzare con la UpdateCommand proprietà per eseguire l'operazione di aggiornamento del database. Se non sono presenti chiavi associate alla query o se UpdateCommand non è una query SQL con parametri, passare null.
- values
- IDictionary
Oggetto IDictionary di valori da utilizzare con la UpdateCommand proprietà per eseguire l'operazione di aggiornamento del database. Se non sono presenti parametri associati alla query o se UpdateCommand non è una query SQL con parametri, passare null.
- oldValues
- IDictionary
Oggetto IDictionary che rappresenta i valori originali nel database. Se non sono presenti parametri associati alla query o se UpdateCommand non è una query SQL con parametri, passare null.
Restituisce
Valore che rappresenta il numero di righe aggiornate nel database sottostante.
Eccezioni
Impossibile SqlDataSource stabilire una connessione con l'origine dati sottostante.
La CanUpdate proprietà è false.
Esempio
Nell'esempio di codice seguente viene illustrato come usare un SqlDataSource controllo per visualizzare i dati in un DropDownList controllo e aggiornare i dati quando si fa clic sul pulsante Invia . La UpdateCommand proprietà viene impostata con un'istruzione SQL con parametri e due ControlParameter parametri vengono aggiunti alla UpdateParameters raccolta. Quando si fa clic sul pulsante Invia , l'evento OnClick viene gestito per chiamare il Update metodo in modo esplicito.
<%@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>
Commenti
Il Update metodo chiama il ExecuteUpdate metodo , passando i keysparametri , valuese oldValues . Per altre informazioni, vedere SqlDataSource.UpdateCommand.