如何在使用存储过程插入数据后显示id,我的示例存储过程是这样的..
CREATE PROCEDURE [dbo].[insertResidence]
@Fname varchar(50),
@Lname varchar(50),
@Mname varchar(50),
@Bdate varchar(50),
@BirthPlace varchar(50),
@CivStat varchar(50),
@Purok varchar(50),
@Religion varchar(50),
@Gender varchar(50),
@CitShip varchar(50)
AS
Begin tran one;
Declare @resId INT;
INSERT INTO [tbl_Residence] (resFname, resLname, resMname, resBdate, resBirthPlace, resGender) VALUES (@Fname,@Lname,@Mname,@Bdate,@BirthPlace,@Gender);
Select @resId = SCOPE_IDENTITY();
NSERT INTO [tbl_Religion] (resId, religion) VALUES (@resId,@Religion);
INSERT INTO [tbl_Purok] (resId,pur_sit) VALUES (@resId,@Purok);
INSERT INTO [tbl_Citizenship] (resId,citship) VALUES (@resID,@Purok);
Select resId from tbl_Residence WHERE resId = @resId;
commit tran;
RETURN
插入数据后,我想在标签或文本文本中显示id。
我在交易中使用ExecuteNonQuery()
..
这是在事务中插入数据的代码
public int insertRes(Informations info)
{
//Insert Residence Informations
SqlCommand cmd = new SqlCommand("InsertResidence");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@FName", SqlDbType.VarChar).Value = info.Fname;
cmd.Parameters.AddWithValue("@LName", SqlDbType.VarChar).Value = info.Lname;
cmd.Parameters.AddWithValue("@MName", SqlDbType.VarChar).Value = info.Mname;
cmd.Parameters.AddWithValue("@BDate", SqlDbType.VarChar).Value = info.Bdate;
cmd.Parameters.AddWithValue("@BirthPlace", SqlDbType.VarChar).Value = info.BirthPlace;
cmd.Parameters.AddWithValue("@CivStat", SqlDbType.VarChar).Value = info.CivStat;
cmd.Parameters.AddWithValue("@Purok", SqlDbType.VarChar).Value = info.purok;
cmd.Parameters.AddWithValue("@Religion", SqlDbType.VarChar).Value = info.Religion;
cmd.Parameters.AddWithValue("@Gender", SqlDbType.VarChar).Value = info.Gender;
cmd.Parameters.AddWithValue("@CitShip", SqlDbType.VarChar).Value = info.CitShip;
return db.ExeNonQuery(cmd);
}
UI图层:
业务层:
答案 0 :(得分:1)
ExecuteNonQuery只会返回您的命令影响的行数。 使用ExecuteScalar,它将返回resId。
显示:
int resId = (int)cmd.ExecuteScalar();
然后在你的用户界面中:
lblMyLabel.Text = resId.ToString();