SqlDataSource.Insert 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
使用 InsertCommand SQL 字符串和集合中的任何 InsertParameters 参数执行插入操作。
public:
int Insert();
public int Insert();
member this.Insert : unit -> int
Public Function Insert () As Integer
返回
一个值,表示插入到基础数据库中的行数。
例外
无法 SqlDataSource 与基础数据源建立连接。
示例
下面的代码示例演示如何使用 SqlDataSource 控件和简单的 Web 窗体页将数据插入数据库。 数据表中的当前数据显示在控件中 DropDownList 。 可以通过在 TextBox 控件中输入值并单击“ 插入 ”按钮来添加新记录。 单击“ 插入 ”按钮时,指定的值将插入到数据库中,然后 DropDownList 刷新。
Important
此示例包含一个接受用户输入的文本框,这是潜在的安全威胁,值插入到参数中而不进行验证,这也是潜在的安全威胁。 在执行查询之前, Inserting 使用事件验证参数值。 有关详细信息,请参阅 脚本攻击概述。
注释
此示例演示如何使用声明性语法进行数据访问。 有关如何使用代码而不是标记访问数据的信息,请参阅 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。