SqlDataSource.InsertParameters Propriedade
Definição
Obtém a coleção de parâmetros que contém os parâmetros usados pela propriedade InsertCommand do objeto SqlDataSourceView associado ao controle SqlDataSource.Gets the parameters collection that contains the parameters that are used by the InsertCommand property from the SqlDataSourceView object that is associated with the SqlDataSource control.
public:
property System::Web::UI::WebControls::ParameterCollection ^ InsertParameters { System::Web::UI::WebControls::ParameterCollection ^ get(); };
[System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)]
public System.Web.UI.WebControls.ParameterCollection InsertParameters { get; }
[<System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)>]
member this.InsertParameters : System.Web.UI.WebControls.ParameterCollection
Public ReadOnly Property InsertParameters As ParameterCollection
Valor da propriedade
Um ParameterCollection que contém os parâmetros usados pela propriedade InsertCommand.A ParameterCollection that contains the parameters used by the InsertCommand property.
- Atributos
Exemplos
O exemplo de código a seguir demonstra como inserir dados em um banco de dado usando o SqlDataSource controle e uma página de Web Forms simples.The following code example demonstrates how to insert data into a database using the SqlDataSource control and a simple Web Forms page. Os dados atuais na tabela de dados são exibidos no DropDownList controle.The current data in the Data table is displayed in the DropDownList control. Você pode adicionar novos registros inserindo valores nos TextBox controles e, em seguida, clicando no botão Inserir .You can add new records by entering values in the TextBox controls, and then clicking the Insert button. Quando o botão de inserção é clicado, os valores especificados são inseridos no banco de dados e o DropDownList controle é atualizado.When the Insert button is clicked, the specified values are inserted into the database, and the DropDownList control is refreshed.
Importante
Este exemplo inclui uma caixa de texto que aceita a entrada do usuário, que é uma possível ameaça à segurança, e os valores são inseridos em parâmetros sem validação, o que também é uma possível ameaça à segurança.This example includes a text box that accepts user input, which is a potential security threat, and values are inserted into parameters without validation, which is also a potential security threat. Use o Inserting evento para validar valores de parâmetro antes de executar a consulta.Use the Inserting event to validate parameter values before executing the query. Para obter mais informações, consulte Visão geral de explorações de script.For more information, see Script Exploits Overview.
Observação
Este exemplo mostra como usar a sintaxe declarativa para acesso a dados.This example shows how to use declarative syntax for data access. Para obter informações sobre como acessar dados usando código em vez de marcação, consulte acessando dados no Visual Studio.For information about how to access data by using code instead of markup, see Accessing data in 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>
Comentários
A InsertParameters propriedade recupera a InsertParameters Propriedade contida pelo SqlDataSourceView objeto que está associado ao SqlDataSource controle.The InsertParameters property retrieves the InsertParameters property that is contained by the SqlDataSourceView object that is associated with the SqlDataSource control.
Se a InsertCommand Propriedade contiver uma consulta SQL parametrizada, a InsertParameters coleção conterá todos os Parameter objetos que correspondam aos espaços reservados de parâmetro na cadeia de caracteres SQL.If the InsertCommand property contains a parameterized SQL query, the InsertParameters collection contains any Parameter objects that correspond to the parameter placeholders in the SQL string.
Dependendo do provedor ADO.NET, a ordem dos parâmetros na InsertParameters coleção pode ser importante.Depending on the ADO.NET provider, the order of the parameters in the InsertParameters collection might be important. Os System.Data.OleDb System.Data.Odbc provedores e associam os parâmetros na coleção de acordo com a ordem em que os parâmetros aparecem na consulta SQL parametrizada.The System.Data.OleDb and System.Data.Odbc providers associate the parameters in the collection according to the order that the parameters appear in the parameterized SQL query. O System.Data.SqlClient provedor, que é o provedor ADO.NET padrão para o SqlDataSource controle, associa os parâmetros na coleção, correspondendo ao nome do parâmetro com um alias de espaço reservado na consulta SQL.The System.Data.SqlClient provider, which is the default ADO.NET provider for the SqlDataSource control, associates the parameters in the collection by matching the name of the parameter with a placeholder alias in the SQL query. Para obter mais informações sobre consultas SQL parametrizadas e comandos, consulte usando parâmetros com o controle SqlDataSource.For more information about parameterized SQL queries and commands, see Using Parameters with the SqlDataSource Control.
Importante
Os valores são inseridos em parâmetros sem validação, que é uma possível ameaça à segurança.Values are inserted into parameters without validation, which is a potential security threat. Use o Filtering evento para validar valores de parâmetro antes de executar a consulta.Use the Filtering event to validate parameter values before executing the query. Para obter mais informações, consulte Visão geral de explorações de script.For more information, see Script Exploits Overview.