gpt4 book ai didi

c# - ASP.NET 插入问题

转载 作者:行者123 更新时间:2023-11-30 17:18:16 25 4
gpt4 key购买 nike

在拥有几年 PHP 经验后,我正尝试开始学习 ASP.NET,但我很难掌握基础知识。

我只是想用 ASP.NET 和 C# 将一行插入到 MS SQL Server Compact 4.0 数据库中。但是,我的页面只插入一个空行。 GridView 的编辑和删除功能正常工作。我做错了什么?

Default.aspx

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="testSite._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>People</h2>
<p>
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True"
ShowSelectButton="True" />
<asp:BoundField DataField="first" HeaderText="first" SortExpression="first" />
<asp:BoundField DataField="last" HeaderText="last" SortExpression="last" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:sqltestConnectionString %>"
DeleteCommand="DELETE FROM [People] WHERE [ID] = @ID"
InsertCommand="INSERT INTO [People] ([first], [last]) VALUES (@first, @last)"
SelectCommand="SELECT * FROM [People]"
UpdateCommand="UPDATE [People] SET [first] = @first, [last] = @last WHERE [ID] = @ID" >
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:FormParameter Name="first" FormField="txtFirst" />
<asp:FormParameter Name="last" FormField="txtLast" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="first" Type="String" />
<asp:Parameter Name="last" Type="String" />
<asp:Parameter Name="ID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
</p>
<p>
<asp:Label ID="Label1" runat="server" Text="First"></asp:Label>
<asp:TextBox ID="txtFirst" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label2" runat="server" Text="Last"></asp:Label>
<asp:TextBox ID="txtLast" runat="server"></asp:TextBox>
<br />
<asp:Button ID="btnAdd" runat="server" onclick="btnAdd_Click" Text="Add" />
</p>
</asp:Content>

Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace testSite
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void btnAdd_Click(object sender, EventArgs e)
{
SqlDataSource1.Insert();
}
}
}

最佳答案

试试这个

    private List<SqlParameter> insertParameters = new List<SqlParameter>();

protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
SqlParameter fName = new SqlParameter("@First", SqlDbType.VarChar, 30);
fName.Direction = ParameterDirection.Input;

fName.Value = txtFirst.Text;
SqlParameter lName = new SqlParameter("@Last", SqlDbType.VarChar, 30);
lName.Direction = ParameterDirection.Input;

lName.Value = txtLast.Text;

insertParameters.Add(fName);
insertParameters.Add(lName);
SqlDataSource1.Insert();
}

protected void SqlDataSource1_Inserting(object sender, SqlDataSourceCommandEventArgs e)
{

e.Command.Parameters.Clear();

foreach (SqlParameter p in insertParameters)

e.Command.Parameters.Add(p);
}
}

关于c# - ASP.NET 插入问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5843796/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com