如何将@@ Identity或Scope_Identity返回到C#

时间:2011-09-27 16:46:45

标签: c# asp.net sql-server-2008

  

可能重复:
  Retrieving the value of RETURN @@IDENTITY in C#

我有一个具有不同方法和构造函数的类。在SaveDB()中,我选择@@ IDENTITY,但我不知道如何将其返回到C#中。我在Stack上看到有关于此的其他帖子,但我没有看到C#的任何直接答案。任何帮助表示赞赏。

我正在寻找的结果看起来像这样:

     int mySiteUserID;

    mySiteUserID = 4; //4 is the Identity of the last saved SiteUser
    public class SiteUser
    {


    #region Fields
    private int siteUserID;
    private string siteUserFirstName;
    private string siteUserLastName;
    private string siteUserTitle;
    private string siteUserEmail;
    private string siteUserPhone;
    private string siteUserPassword;
    private int siteUserType;
    private int companyID;
    private bool siteUserActive;
    #endregion

    #region Properties
    public int SiteUserID { get { return siteUserID; } set { siteUserID = value; } }
    public string SiteUserFirstName { get { return siteUserFirstName; } set { siteUserFirstName = value; } }
    public string SiteUserLastName { get { return siteUserLastName; } set { siteUserLastName = value; } }
    public string SiteUserTitle { get { return siteUserTitle; } set { siteUserTitle = value; } }
    public string SiteUserEmail { get { return siteUserEmail; } set { siteUserEmail = value; } }
    public string SiteUserPhone { get { return siteUserPhone; } set { siteUserPhone = value; } }
    public string SiteUserPassword { get { return siteUserPassword; } set { siteUserPassword = value; } }
    public int SiteUserType { get { return siteUserType; } set { siteUserType = value; } }
    public int CompanyID { get { return companyID; } set { companyID = value; } }
    public bool SiteUserActive { get { return siteUserActive; } set { siteUserActive = value; } }
    #endregion

    #region Constructors
    public SiteUser()
    {
        siteUserID = 0;
        siteUserFirstName = "";
        siteUserLastName = "";
        siteUserTitle = "";
        siteUserEmail = "";
        siteUserPhone = "";
        siteUserPassword = "";
        siteUserType = 0;
        companyID = 0;
        siteUserActive = false;
    }
    public SiteUser(int PrimaryKeyValue)
    {
        string sql = @"
        SELECT *
        FROM SiteUsers
        WHERE SiteUserID = @PrimaryKeyValue";
        SqlParameter[] parms = new SqlParameter[1];
        parms[0] = new SqlParameter("@PrimaryKeyValue", PrimaryKeyValue);

        SqlDataReader dr = DBUtil.FillDataReader(sql, parms);

        while (dr.Read())
        {
            this.siteUserID = Convert.ToInt32(dr["siteUserID"].ToString());
            this.siteUserFirstName = Convert.ToString(dr["siteUserFirstName"].ToString());
            this.siteUserLastName = Convert.ToString(dr["siteUserLastName"].ToString());
            this.siteUserTitle = Convert.ToString(dr["siteUserTitle"].ToString());
            this.siteUserEmail = Convert.ToString(dr["siteUserEmail"].ToString());
            this.siteUserPhone = Convert.ToString(dr["siteUserPhone"].ToString());
            this.siteUserPassword = Convert.ToString(dr["siteUserPassword"].ToString());
            this.siteUserType = Convert.ToInt32(dr["siteUserType"].ToString());
            this.companyID = Convert.ToInt32(dr["companyID"].ToString());
            this.siteUserActive = Convert.ToBoolean(dr["siteUserActive"].ToString());
        }
        dr.Close();
    }

    #endregion


    public int SaveDB()
    {
        //db code to write record
        SqlConnection conn = new SqlConnection(Config.ConnectionString);

        string sql = "";


        if (SiteUserID == 0)
        {
            //this is an insert
            sql = @"
                INSERT SiteUsers(
    --SiteUserID,
    SiteUserFirstName,
    SiteUserLastName,
    SiteUserTitle,
    SiteUserEmail,
    SiteUserPhone,
    SiteUserPassword,
    SiteUserType,
    CompanyID,
    SiteUserActive

                 )VALUES(
    --@SiteUserID,
    @SiteUserFirstName,
    @SiteUserLastName,
    @SiteUserTitle,
    @SiteUserEmail,
    @SiteUserPhone,
    @SiteUserPassword,
    @SiteUserType,
    @CompanyID,
    @SiteUserActive

                );
    SELECT @@IDENTITY
    ";

        }
        else
        {
            //this is an update //siteUserID = @siteUserID
            //this is an update //siteUserFirstName = @siteUserFirstName
            //this is an update //siteUserLastName = @siteUserLastName
            //this is an update //siteUserTitle = @siteUserTitle
            //this is an update //siteUserEmail = @siteUserEmail
            //this is an update //siteUserPhone = @siteUserPhone
            //this is an update //siteUserPassword = @siteUserPassword
            //this is an update //siteUserType = @siteUserType
            //this is an update //companyID = @companyID
            //this is an update //siteUserActive = @siteUserActive
            sql = @"
            UPDATE SiteUsers
                SET 
    --SiteUserID = @SiteUserID,
    SiteUserFirstName = @SiteUserFirstName,
    SiteUserLastName = @SiteUserLastName,
    SiteUserTitle = @SiteUserTitle,
    SiteUserEmail = @SiteUserEmail,
    SiteUserPhone = @SiteUserPhone,
    SiteUserPassword = @SiteUserPassword,
    SiteUserType = @SiteUserType,
    CompanyID = @CompanyID,
    SiteUserActive = @SiteUserActive

                WHERE SiteUserID = 
    @SiteUserID
                                        ";
        }

        SqlCommand cmd = new SqlCommand(sql, conn);

        SqlParameter siteUserIDParam = new SqlParameter("@siteUserID", SqlDbType.Int);
        SqlParameter siteUserFirstNameParam = new SqlParameter("@siteUserFirstName", SqlDbType.VarChar);
        SqlParameter siteUserLastNameParam = new SqlParameter("@siteUserLastName", SqlDbType.VarChar);
        SqlParameter siteUserTitleParam = new SqlParameter("@siteUserTitle", SqlDbType.VarChar);
        SqlParameter siteUserEmailParam = new SqlParameter("@siteUserEmail", SqlDbType.VarChar);
        SqlParameter siteUserPhoneParam = new SqlParameter("@siteUserPhone", SqlDbType.VarChar);
        SqlParameter siteUserPasswordParam = new SqlParameter("@siteUserPassword", SqlDbType.VarChar);
        SqlParameter siteUserTypeParam = new SqlParameter("@siteUserType", SqlDbType.Int);
        SqlParameter companyIDParam = new SqlParameter("@companyID", SqlDbType.Int);
        SqlParameter siteUserActiveParam = new SqlParameter("@siteUserActive", SqlDbType.Bit);

        siteUserIDParam.Value = siteUserID;
        siteUserFirstNameParam.Value = siteUserFirstName;
        siteUserLastNameParam.Value = siteUserLastName;
        siteUserTitleParam.Value = siteUserTitle;
        siteUserEmailParam.Value = siteUserEmail;
        siteUserPhoneParam.Value = siteUserPhone;
        siteUserPasswordParam.Value = siteUserPassword;
        siteUserTypeParam.Value = siteUserType;
        companyIDParam.Value = companyID;
        siteUserActiveParam.Value = siteUserActive;

        cmd.Parameters.Add(siteUserIDParam);
        cmd.Parameters.Add(siteUserFirstNameParam);
        cmd.Parameters.Add(siteUserLastNameParam);
        cmd.Parameters.Add(siteUserTitleParam);
        cmd.Parameters.Add(siteUserEmailParam);
        cmd.Parameters.Add(siteUserPhoneParam);
        cmd.Parameters.Add(siteUserPasswordParam);
        cmd.Parameters.Add(siteUserTypeParam);
        cmd.Parameters.Add(companyIDParam);
        cmd.Parameters.Add(siteUserActiveParam);

        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
        return 0;
    }

    }

1 个答案:

答案 0 :(得分:1)

执行脚本时,请使用ExecuteScalar代替ExecuteNonQuery,并将结果转换为int。该脚本仍会产生插入行的副作用,但它的值将是作用域标识。