让我们说,我有一个2 TextBox,一个按钮和一个DataSource。这就是我要的。当我单击按钮时,DataSource从两个文本框中收集数据并将它们插入数据库。
以下是代码:如何插入联系人?
<script runat="server">
protected void SaveButton_Click(object sender, EventArgs e)
{
}
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<!-- TextBoxe-->
First Name: <asp:TextBox ID="FirstNameTextBox" runat="server"/><br />
Last Name: <asp:TextBox ID="LastNameTextBox" runat="server"/><br />
<!-- Bouton -->
<asp:Button ID="SaveButton" runat="server" Text="Save"
onclick="SaveButton_Click" />
<!-- DataSource -->
<asp:SqlDataSource ID="ContactSqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:ContactConnectionString %>"
InsertCommand="INSERT INTO [Contacts] ([FirstName], [LastName]) VALUES FirstName, @LastName)"
SelectCommand="SELECT * FROM [Contacts]">
<InsertParameters>
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="LastName" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
答案 0 :(得分:1)
只需你可以做到这一点......
protected void Button1_Click(object sender, EventArgs e)
{
SqlDataSource1.InsertParameters.Add("ParameterName", "ParameterValue");
......Set parameters for Insert....................
..........................
SqlDataSource1.Insert(); // then Call the insert method to perform insertion
}