我想在名为
Comment
的 SQL Server 2008 数据库表中插入一行,然后使用该插入行的 id 用新数据行填充第二个表 (CommentOtherAuthor
)。基本上,一条评论可以有多个作者。
这是代码:
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。
我只是不太明白如何从评论表中获取最后一个插入而不进行新的操作
SELECT TOP 1 id FROM Comment ORDER BY id DESC
我认为这不是最好的方法。
任何人都可以建议如何让它工作吗?
非常感谢!
该
SELECT TOP 1 id ...
查询很可能不会在负载下的系统中为您提供正确的结果。如果您有 20 或 50 个客户同时插入评论,那么当您再次查询该表时,您很有可能会得到其他人的 id
...
我认为做到这一点的最好方法是:
OUTPUT
子句并捕获新插入的 ID
大致如下:
c.Id = sqlHelper.ExecuteScalar<int>(
@"insert into Comment(......)
output Inserted.ID
values(.............)",
使用这种方法,您的
c.Id
值现在应该是新插入的 ID
- 在下一个插入语句中使用 that! (注意:现在,您可能总是会得到一个 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!
);
}
假设您使用的是 Microsoft SQL Server,您可以设计表 Comment,以便列 Id 将属性 Identity 设置为 true。这样,每次向表中插入一行时,数据库都会生成并自动递增 id。
您必须在 SQL 请求中使用以下行:
输出已插入.Id
以便在执行请求时将此 Id 返回到您的 C# 代码。