我不擅长 .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
这并没有让我觉得这是做到这一点的最佳方式。
谁能建议如何使它工作?
非常感谢!