可以向一个表中添加一条记录,在取出新添加记录的ID,把ID添到另个表 ;
类中的方法
public void add()
{
int rowsAffected;
SqlParameter[] parameters = {
new SqlParameter("@DTitle",_dtitle),
new SqlParameter("@Dcontent", _dcontent),
new SqlParameter("@UserName", _username),
new SqlParameter("@Ddate",_ddate),
new SqlParameter("@DIspublic",_dispublic),
new SqlParameter("@RealName",_realname),
new SqlParameter("@DNumber", "0"),
new SqlParameter("@DiaryType",_diarytype),
new SqlParameter("@DIsdel","0"),
new SqlParameter("@MType",_MyknowID),
new SqlParameter("@isInKnow",_isinknow)
};
sdh.RunProcedure("[addDiary]", parameters, out rowsAffected);
{
int rowsAffected;
SqlParameter[] parameters = {
new SqlParameter("@DTitle",_dtitle),
new SqlParameter("@Dcontent", _dcontent),
new SqlParameter("@UserName", _username),
new SqlParameter("@Ddate",_ddate),
new SqlParameter("@DIspublic",_dispublic),
new SqlParameter("@RealName",_realname),
new SqlParameter("@DNumber", "0"),
new SqlParameter("@DiaryType",_diarytype),
new SqlParameter("@DIsdel","0"),
new SqlParameter("@MType",_MyknowID),
new SqlParameter("@isInKnow",_isinknow)
};
sdh.RunProcedure("[addDiary]", parameters, out rowsAffected);
}
数据库中的操作
ALTER PROCEDURE [dbo].[addDiary]
(@DTitle varchar(250),
@Dcontent text,
@UserName nvarchar(256),
@Ddate int,
@DIspublic int,
@RealName nvarchar(256),
@DNumber int,
@DiaryType int,
@DIsdel int,
@MType int,
@isInKnow int
(@DTitle varchar(250),
@Dcontent text,
@UserName nvarchar(256),
@Ddate int,
@DIspublic int,
@RealName nvarchar(256),
@DNumber int,
@DiaryType int,
@DIsdel int,
@MType int,
@isInKnow int
)
AS
declare @newID int
BEGIN
if @isInKnow=0
begin
insert into DS_diary(Dtitle,Dcontent,UserName,Ddate,DIspublic,RealName,DNumber,DiaryType,DIsdel) values (@DTitle,@Dcontent,@UserName,@Ddate,@DIspublic,@RealName,@DNumber,@DiaryType,@DIsdel)
end
else
begin
insert into DS_diary(Dtitle,Dcontent,UserName,Ddate,DIspublic,RealName,DNumber,DiaryType,DIsdel) values (@DTitle,@Dcontent,@UserName,@Ddate,@DIspublic,@RealName,@DNumber,@DiaryType,@DIsdel)
select @newID=(select @@Identity)
insert into DS_MyKnow(MType,ContentId,IsQa,UserName) vaues (@MType,@newID,'0',@UserName)
end
AS
declare @newID int
BEGIN
if @isInKnow=0
begin
insert into DS_diary(Dtitle,Dcontent,UserName,Ddate,DIspublic,RealName,DNumber,DiaryType,DIsdel) values (@DTitle,@Dcontent,@UserName,@Ddate,@DIspublic,@RealName,@DNumber,@DiaryType,@DIsdel)
end
else
begin
insert into DS_diary(Dtitle,Dcontent,UserName,Ddate,DIspublic,RealName,DNumber,DiaryType,DIsdel) values (@DTitle,@Dcontent,@UserName,@Ddate,@DIspublic,@RealName,@DNumber,@DiaryType,@DIsdel)
select @newID=(select @@Identity)
insert into DS_MyKnow(MType,ContentId,IsQa,UserName) vaues (@MType,@newID,'0',@UserName)
end
END
select @@Identity是新的自增ID