3

我不擅长 .NET,但正在学习(至少尝试学习!;))。但是,我正在编写的这段代码让我感到困惑。我想要做的是在名为 的 SQL Server 2008 数据库表Comment中插入一行,然后使用此插入行的 idCommentOtherAuthor用新的数据行填充第二个表 ( )。基本上,一条评论可以有多个作者。

这是代码:

public static Comment MakeNew(int parentNodeId, string firstname, string surname, string occupation, string affiliation, string title, string email, bool publishemail, bool competinginterests, string competingintereststext, string[] otherfirstname, string[] othersurname, string[] otheroccupation, string[] otheraffiliation, string[] otheremail, bool approved, bool spam, DateTime created, string commentText, int statusId)
{
        var c = new Comment
            {
                ParentNodeId = parentNodeId,
                FirstName = firstname,
                Surname = surname,
                Occupation = occupation,
                Affiliation = affiliation,
                Title = title,
                Email = email,
                PublishEmail = publishemail,
                CompetingInterests = competinginterests,
                CompetingInterestsText = competingintereststext,
                OtherFirstName = otherfirstname,
                OtherSurname = othersurname,
                OtherOccupation = otheroccupation,
                OtherAffiliation = otheraffiliation,
                OtherEmail = otheremail,
                Approved = approved,
                Spam = spam,
                Created = created,
                CommenText = commentText,
                StatusId = statusId
            };

        var sqlHelper = DataLayerHelper.CreateSqlHelper(umbraco.GlobalSettings.DbDSN);

        c.Id = sqlHelper.ExecuteScalar<int>(
            @"insert into Comment(mainid,nodeid,firstname,surname,occupation,affiliation,title,email,publishemail,competinginterests,competingintereststext,comment,approved,spam,created,statusid) 
                values(@mainid,@nodeid,@firstname,@surname,@occupation,@affiliation,@title,@email,@publishemail,@competinginterests,@competingintereststext,@comment,@approved,@spam,@created,@statusid)",
            sqlHelper.CreateParameter("@mainid", -1),
            sqlHelper.CreateParameter("@nodeid", c.ParentNodeId),
            sqlHelper.CreateParameter("@firstname", c.FirstName),
            sqlHelper.CreateParameter("@surname", c.Surname),
            sqlHelper.CreateParameter("@occupation", c.Occupation),
            sqlHelper.CreateParameter("@affiliation", c.Affiliation),
            sqlHelper.CreateParameter("@title", c.Title),
            sqlHelper.CreateParameter("@email", c.Email),
            sqlHelper.CreateParameter("@publishemail", c.PublishEmail),
            sqlHelper.CreateParameter("@competinginterests", c.CompetingInterests),
            sqlHelper.CreateParameter("@competingintereststext", c.CompetingInterestsText),
            sqlHelper.CreateParameter("@comment", c.CommenText),
            sqlHelper.CreateParameter("@approved", c.Approved),
            sqlHelper.CreateParameter("@spam", c.Spam),
            sqlHelper.CreateParameter("@created", c.Created),
            sqlHelper.CreateParameter("@statusid", c.StatusId));

        c.OnCommentCreated(EventArgs.Empty);

        for (int x = 0; x < otherfirstname.Length; x++)
        {
            sqlHelper.ExecuteScalar<int>(
                @"insert into CommentOtherAuthor(firstname,surname,occupation,affiliation,email,commentid) values(@firstname,@surname,@occupation,@affiliation,@email,@commentid)",
                sqlHelper.CreateParameter("@firstname", otherfirstname[x]),
                sqlHelper.CreateParameter("@surname", othersurname[x]),
                sqlHelper.CreateParameter("@occupation", otheroccupation[x]),
                sqlHelper.CreateParameter("@affiliation", otheraffiliation[x]),
                sqlHelper.CreateParameter("@email", otheremail[x]),
                sqlHelper.CreateParameter("@commentid", 123)
            );
        }

        if (c.Spam)
        {
            c.OnCommentSpam(EventArgs.Empty);
        }

        if (c.Approved)
        {
            c.OnCommentApproved(EventArgs.Empty);
        }

        return c;
    }

关键线是:

sqlHelper.CreateParameter("@commentid", 123)

目前,我只是将评论的 id 硬编码为 123,但实际上我需要它是刚刚插入评论表的记录的 id。

我只是不太明白如何在不执行新操作的情况下从表 Comment 中获取最后一个插入

SELECT TOP 1 id FROM Comment ORDER BY id DESC

这并没有让我觉得这是做到这一点的最佳方式。

谁能建议如何使它工作?

非常感谢!

4

2 回答 2

4

在负载下的系统中,该SELECT TOP 1 id ...查询很可能不会给您正确的结果。如果您有 20 或 50 个客户同时插入评论,那么当您再次查询该表时,您很有可能会得到其他人的id...

我认为最好的方法是:

  • 在原始插入中添加一个OUTPUT子句并捕获新插入的ID
  • 使用该 ID 进行第二次插入

类似于以下内容:

c.Id = sqlHelper.ExecuteScalar<int>(
        @"insert into Comment(......) 
          output Inserted.ID                
          values(.............)",

使用这种方法,您的c.Id值现在应该是新插入的ID-在您的下一个插入语句中使用它!注意:现在,您可能总是会得到1回报 - 受您的语句影响的行数......

这种方法假定您的表Comment有一个类型的列,INT IDENTITY当您在其中插入新行时会自动设置该列。

for (int x = 0; x < otherfirstname.Length; x++)
{
        sqlHelper.ExecuteScalar<int>(
            @"insert into CommentOtherAuthor(.....) values(.....)",
            sqlHelper.CreateParameter("@firstname", otherfirstname[x]),
            sqlHelper.CreateParameter("@surname", othersurname[x]),
            sqlHelper.CreateParameter("@occupation", otheroccupation[x]),
            sqlHelper.CreateParameter("@affiliation", otheraffiliation[x]),
            sqlHelper.CreateParameter("@email", otheremail[x]),
            sqlHelper.CreateParameter("@commentid", c.Id)  <<=== use that value you got back!
        );
}
于 2012-06-02T21:24:00.447 回答
0

假设您使用的是 Microsoft SQL Server,您可以设计您的表Comment,以便Id列的属性Identity设置为 true。这样,每次将行插入表中时,数据库都会生成并自动增加 id。

您必须在 SQL 请求中使用以下行:

输出插入.Id

为了在执行请求时将此 ID 返回给您的 C# 代码。

于 2012-06-02T21:39:47.630 回答