0

如何在特定帖子上插入特定评论?(如 Facebook)我在 sql 中有两个表

  1. wall=>wallid,用户名,帖子
  2. 发表评论=>墙ID,评论,评论ID

当我手动插入墙 ID 时,它工作正常,但我希望墙 ID 应该自动生成到我输入评论的帖子中。

在设计部分

<div style="text-align: center">

        <asp:TextBox ID="TextBox1" runat="server" Height="61px" TextMode="MultiLine" Width="271px"></asp:TextBox>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <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>
4

2 回答 2

0

您的架构应该看起来像

1. Post -> PostId,WallId,PostedBy, Post,PostedDateTime
2. Comments -> CommentId,PostId,Comments,CommentedBy,CommentedDateTime

Post 和 Wall 之间的关系应该是 1 Wall 和 Many Posts 是 1:M 的关系,评论相似,1:M。

保存时

请执行下列操作:

  1. 自动生成 PostId。
  2. 从查询字符串或会话发送 WallId 或通过任何您正在识别朋友墙的方式发送。同样找出拥有帖子的当前用户 ID 并保存帖子及其日期时间。
  3. 在评论上,在保存评论时,获取 postid 和您自己的用户 id,并让评论 id 自动递增。
于 2013-10-05T12:08:07.600 回答
0

将参数方向作为 InputOutput 参数。

        <asp:Parameter Name="wallID" Type="Int32" Direction="InputOutput"></asp:Parameter>

然后你可以在插入事件上使用:

        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

于 2013-10-05T12:10:26.653 回答