私は.NETが得意ではありませんが、学んでいます(少なくともしようとしています!;))。しかし、私が取り組んでいるこのコードのビットは私を困惑させました。私がやりたいのは、と呼ばれるSQL Server 2008データベーステーブルにComment
行を挿入し、この挿入された行のIDを使用して、2番目のテーブル(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
これを行うための最良の方法として私を襲うことはありません。
誰かがこれを機能させる方法を提案できますか?
どうもありがとう!