2012-11-16 104 views
0

我需要在C#代碼插入數據之後從存儲過程取scope_identity()值從存儲過程抓取在C#代碼SCOPE_IDENTITY值。在3層架構

這裏我使用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值。任何人幫助?

回答

0

您需要獲得通過輸出參數值,所以你必須遵循以下鏈接:
Get output parameter value in ADO.NET
How to use OUTPUT parameter in Stored Procedure

你只需要創建一個的SqlParameter時,方向設置爲輸出, 並將其添加到SqlCommand的Parameters集合。然後執行 存儲過程並獲取參數的值。

同時添加在你的方法加入額外的參數後,參數做如下

param.AddParam(SqlDbType.Int, "@c_Id", IdValue, ParameterDirection.Output); 

然後訪問您的輸出參數如下價值:

object value = cmd.Parameters["@c_Id"].Value; 

來源:Getting the identity of the most recently added record 代碼片段:

string query = "AddCategory"; 
int ID; 
string connect = @"Server=.\SQLExpress;Database=Northwind;Trusted_Connection=Yes;"; 
using (SqlConnection conn = new SqlConnection(connect)) 
{ 
    using (SqlCommand cmd = new SqlCommand(query, conn)) 
    { 
    cmd.CommandType = CommandType.StoredProcedure; 
    cmd.Parameters.AddWithValue("@Category", Category.Text); 
    cmd.Parameters.Add("@CategoryID", SqlDbType.Int, 0, "CategoryID"); 
    cmd.Parameters["@CategoryID"].Direction = ParameterDirection.Output; 
    conn.Open(); 
    cmd.ExecuteNonQuery(); 
    ID = (int)cmd.Parameters["@CategoryID"].Value; 
    } 
} 

程序...

CREATE PROCEDURE AddCategory 
    -- Add the parameters for the stored procedure here 
    @Category NVARCHAR(15), 
    @CategoryID INT OUTPUT 
AS 
BEGIN 
    SET NOCOUNT ON; 

    -- Insert statements for procedure here 
    INSERT INTO Categories (CategoryName) VALUES (@Category) 
    SET @CategoryID = SCOPE_IDENTITY() 
END 

希望以上的參考,幫助您更瞭解這一切。

+0

變量'@ n_user_group_id'是存儲過程的輸出參數。 –

+0

@Niranjan卡拉OK。我怎麼能在C#代碼中獲取該值.Bcoz在這裏我在不同的層中使用Sqlcommand,因爲它會被其他函數使用,所以我不能在該階段獲取作用域標識的值? – Nirmala

+0

我已經更新了我的答案..檢查這個。現在它更加相關 –