scope_identity()
C# コードにデータを挿入した後、ストアド プロシージャから値を取得する必要があります。
ここでは 3 層アーキテクチャを使用しています。誰か助けてください
protected void Submit_Click(object sender, EventArgs e)
{
this.CreateUserGroups(strGroupName, strDescription, strGroupType);
}
protected void CreateUserGroups(string strGroupName, string strDescription, string strGroupType)
{
string strReturn = string.Empty;
strReturn = objUser.SaveCreateGroups(strGroupName, strDescription, strGroupType);
}
public string SaveCreateGroups(string strGroupName, string strDescription, string strGroupType)
{
try
{
DataManager.ParamBuilder param = new DataManager.ParamBuilder();
if (strGroupName != string.Empty)
param.AddParam(System.Data.SqlDbType.VarChar, "@c_groupname", strGroupName);
else
param.AddParam(System.Data.SqlDbType.Int, "@c_groupname", DBNull.Value);
if (strDescription != string.Empty)
param.AddParam(System.Data.SqlDbType.VarChar, "@c_description", strDescription);
else
param.AddParam(System.Data.SqlDbType.Int, "@c_description", DBNull.Value);
if (strGroupType != string.Empty)
param.AddParam(System.Data.SqlDbType.Int, "@n_grouptype", strGroupType);
else
param.AddParam(System.Data.SqlDbType.VarChar, "@n_grouptype", DBNull.Value);
String strIsUserExitsInGroup = DataManager.ExecuteScalar("sp_create_user_groups", param.Parameters, true).ToString();
return strIsUserExitsInGroup;
}
catch (Exception e)
{
throw new Exception(e.ToString());
}
}
public static object ExecuteScalar(string procedureNameOrSql, List<SqlParameter> parameters, bool isStoredProcedure)
{
object scalarValue;
using (SqlConnection cn = createConnection())
{
SqlCommand cmd = new SqlCommand(procedureNameOrSql, cn);
if (isStoredProcedure)
cmd.CommandType = CommandType.StoredProcedure;
if (parameters != null)
cmd.Parameters.AddRange(parameters.ToArray());
scalarValue = cmd.ExecuteScalar();
}
return scalarValue;
}
私のストアドプロシージャ:
CREATE Procedure [dbo].[sp_create_user_groups]
@n_user_group_id int OUTPUT,
@c_groupname varchar(200),
@c_description varchar(200),
@n_grouptype int
As
Declare @IsGroupNameExists int,
@b_active bit
select @IsGroupNameExists = 0
set @b_active = 1
BEGIN
IF exists(select top 1 * from gdt_user_groups where c_group_name = @c_groupname)
BEGIN
select @IsGroupNameExists = 1
END
ELSE
BEGIN
SET NOCOUNT ON;
insert into gdt_user_groups(c_group_name,c_description,n_group_id,b_active,d_modified_dttm,
d_created_dttm)values(@c_groupname,@c_description,@n_grouptype,@b_active,GETDATE(),GETDATE())
select @n_user_group_id = SCOPE_IDENTITY();
select @IsGroupNameExists = 0
END
select @IsGroupNameExists
END
ここで正確に、C# コードで scope_identity 値を取得できます。誰か助けて?