如何在特定帖子上插入特定评论? (像Facebook) 我在sql中有两个表
当我手动插入墙壁ID时,它工作正常,但我希望墙壁ID应自动生成到我输入评论的帖子。
在设计部分
<div style="text-align: center">
<asp:TextBox ID="TextBox1" runat="server" Height="61px" TextMode="MultiLine" Width="271px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Height="37px" Text="Submit" Width="91px" OnClick="Button1_Click" />
</div>
<div>
<br />
<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<div style="border: solid 2px brown; width: 400px; margin-bottom: 20px">
<span style="">Id:
<asp:Label ID="IdLabel" runat="server" Text='<%# Eval("wallId") %>' />
<br />
post:
<asp:Label ID="postLabel" runat="server" Text='<%# Eval("post") %>' />
<br />
username:
<asp:Label ID="usernameLabel" runat="server" Text='<%# Eval("username") %>' />
<br />
<br />
</span>
<br />
<hr style="height: 3px; background-color: blue" />
<asp:ListView ID="ListView2" runat="server" DataSourceID="SqlDataSource2" DataKeyNames="commentID" InsertItemPosition="LastItem">
<AlternatingItemTemplate>
<span style="">commentID:<asp:Label Text='<%# Eval("commentID") %>' runat="server" ID="commentIDLabel" />
<br />
wallID:<asp:Label Text='<%# Eval("wallID") %>' runat="server" ID="wallIDLabel" />
<br />
comment:<asp:Label Text='<%# Eval("comment") %>' runat="server" ID="commentLabel" />
<br />
<br />
</span>
<br />
<hr style="height: 3px; background-color: blue" />
</AlternatingItemTemplate>
<EditItemTemplate>
<span style="">commentID:<asp:Label Text='<%# Eval("commentID") %>' runat="server" ID="commentIDLabel1" />
<br />
wallID:<asp:TextBox Text='<%# Bind("wallID") %>' runat="server" ID="wallIDTextBox" />
<br />
comment:<asp:TextBox Text='<%# Bind("comment") %>' runat="server" ID="commentTextBox" />
<br />
<asp:Button runat="server" CommandName="Update" Text="Update" ID="UpdateButton" /><asp:Button runat="server" CommandName="Cancel" Text="Cancel" ID="CancelButton" /><br />
<br />
</span>
<br />
<hr style="height: 3px; background-color: blue" />
</EditItemTemplate>
<EmptyDataTemplate>
<span>No data was returned.</span>
</EmptyDataTemplate>
<InsertItemTemplate>
<span style="">wallID:<asp:TextBox Text='<%# Bind("wallID") %>' runat="server" ID="wallIDTextBox" />
<br />
comment:<asp:TextBox Text='<%# Bind("comment") %>' runat="server" ID="commentTextBox" />
<br />
<asp:Button runat="server" CommandName="Insert" Text="Insert" ID="InsertButton" /><asp:Button runat="server" CommandName="Cancel" Text="Clear" ID="CancelButton" /><br />
<br />
</span>
<br />
<hr style="height: 3px; background-color: blue" />
</InsertItemTemplate>
<ItemTemplate>
<span style="">commentID:<asp:Label Text='<%# Eval("commentID") %>' runat="server" ID="commentIDLabel" />
<br />
wallID:<asp:Label Text='<%# Eval("wallID") %>' runat="server" ID="wallIDLabel" />
<br />
comment:<asp:Label Text='<%# Eval("comment") %>' runat="server" ID="commentLabel" />
<br />
<br />
</span>
<br />
<hr style="height: 3px; background-color: blue" />
</ItemTemplate>
<LayoutTemplate>
<div runat="server" id="itemPlaceholderContainer" style=""><span runat="server" id="itemPlaceholder" /></div>
<div style="">
</div>
</LayoutTemplate>
<SelectedItemTemplate>
<span style="">commentID:<asp:Label Text='<%# Eval("commentID") %>' runat="server" ID="commentIDLabel" />
<br />
wallID:<asp:Label Text='<%# Eval("wallID") %>' runat="server" ID="wallIDLabel" />
<br />
comment:<asp:Label Text='<%# Eval("comment") %>' runat="server" ID="commentLabel" />
<br />
<br />
</span>
<br />
<hr style="height: 3px; background-color: blue" />
</SelectedItemTemplate>
</asp:ListView>
<asp:SqlDataSource runat="server" ID="SqlDataSource2" ConnectionString='<%$ ConnectionStrings:ConnectionString %>' SelectCommand="SELECT * FROM [postcomment]" ConflictDetection="CompareAllValues" DeleteCommand="DELETE FROM [postcomment] WHERE [commentID] = @original_commentID AND [wallID] = @original_wallID AND (([comment] = @original_comment) OR ([comment] IS NULL AND @original_comment IS NULL))" InsertCommand="INSERT INTO [postcomment] ([wallID], [comment]) VALUES (@wallID, @comment)" OldValuesParameterFormatString="original_{0}" UpdateCommand="UPDATE [postcomment] SET [wallID] = @wallID, [comment] = @comment WHERE [commentID] = @original_commentID AND [wallID] = @original_wallID AND (([comment] = @original_comment) OR ([comment] IS NULL AND @original_comment IS NULL))">
<DeleteParameters>
<asp:Parameter Name="original_commentID" Type="Int32"></asp:Parameter>
<asp:Parameter Name="original_wallID" Type="Int32"></asp:Parameter>
<asp:Parameter Name="original_comment" Type="String"></asp:Parameter>
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="wallID" Type="Int32"></asp:Parameter>
<asp:Parameter Name="comment" Type="String"></asp:Parameter>
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="wallID" Type="Int32"></asp:Parameter>
<asp:Parameter Name="comment" Type="String"></asp:Parameter>
<asp:Parameter Name="original_commentID" Type="Int32"></asp:Parameter>
<asp:Parameter Name="original_wallID" Type="Int32"></asp:Parameter>
<asp:Parameter Name="original_comment" Type="String"></asp:Parameter>
</UpdateParameters>
</asp:SqlDataSource>
</ItemTemplate>
</asp:ListView>
<asp:SqlDataSource runat="server" ID="SqlDataSource1" ConnectionString='<%$ ConnectionStrings:ConnectionString %>' SelectCommand="SELECT * FROM [wall]"></asp:SqlDataSource>
</div>
答案 0 :(得分:0)
您的架构应该看起来像
1. Post -> PostId,WallId,PostedBy, Post,PostedDateTime
2. Comments -> CommentId,PostId,Comments,CommentedBy,CommentedDateTime
Post和Wall之间的关系应该是1 Wall和很多帖子是1:M关系,类似于评论,1:M。
执行以下操作:
答案 1 :(得分:0)
将参数方向设为InputOutput参数。
<asp:Parameter Name="wallID" Type="Int32" Direction="InputOutput"></asp:Parameter>
然后你可以使用Inserted事件:
protected void ListView_Inserted(object sender, SqlDataSourceSelectingEventArgs e)
{
var wallID = e.Command.Parameters["@wallID"].Value ;
}
因为您希望将wallid作为标识值自动插入。你必须选择它,以便你可以葡萄它。试试这个
DECLARE @wallID AS INT = -1
INSERT INTO [postcomment]
([comment])
VALUES (@comment)
select @wallID =SCOPE_IDENTITY();
它也不是ListView_Inserted事件,它是SqlDataSource2_Inserted