By using this site, you acknowledge that you have read and understand our Cookie policy, Privacy policy and Terms .
close

首先是存储过程:

USE [VisualWar]

GO

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

ALTER PROCEDURE [dbo].[User_Reg]

@TRIBEID bigint,

@USERNAME nvarchar(128),

@USERPASSWORD nvarchar(128),

@USERQUESTION nvarchar(128),

@USERANSWER nvarchar(128),

@USEREMAIL nvarchar(128),

@ACTIVATIONSTRING nvarchar(128)

AS

BEGIN

    SET NOCOUNT ON;

    INSERT INTO

    U_User(

    TribeID,

    UserName,

    UserPassword,

    UserQuestion,

    UserAnswer,

    UserEmail,

    ActivationString

    )

    VALUES(

    @TRIBEID,

    @USERNAME,

    @USERPASSWORD,

    @USERQUESTION,

    @USERANSWER,

    @USEREMAIL,

    @ACTIVATIONSTRING

    )

END

RETURN @@IDENTITY

我想不用过多解释,主要是最后以行的返回值。

然后具体代码:

        public long Reg(long tribeID, string userName, string userPassword, string userQuestion, string userAnswer, string userEmail, string activationString)

        {

            long userID = 0;

            DataSet dsUser = new DataSet();

            SqlConnection regConn = Conn;

            SqlCommand scSelect = new SqlCommand("User_Reg", regConn);

            scSelect.CommandType = CommandType.StoredProcedure;

            scSelect.Parameters.Add("@TRIBEID", SqlDbType.BigInt);

            scSelect.Parameters.Add("@USERNAME", SqlDbType.NVarChar, 128);

            scSelect.Parameters.Add("@USERPASSWORD", SqlDbType.NVarChar, 128);

            scSelect.Parameters.Add("@USERQUESTION", SqlDbType.NVarChar, 128);

            scSelect.Parameters.Add("@USERANSWER", SqlDbType.NVarChar, 128);

            scSelect.Parameters.Add("@USEREMAIL", SqlDbType.NVarChar, 128);

            scSelect.Parameters.Add("@ACTIVATIONSTRING", SqlDbType.NVarChar, 128);

          scSelect.Parameters.Add(new SqlParameter("ReturnID", SqlDbType.Int, 4, ParameterDirection.ReturnValue, false, 0, 0, string.Empty, DataRowVersion.Default, null));

            scSelect.Parameters["@TRIBEID"].Value = tribeID;

            scSelect.Parameters["@USERNAME"].Value = userName;

            scSelect.Parameters["@USERPASSWORD"].Value = userPassword;

            scSelect.Parameters["@USERQUESTION"].Value = userQuestion;

            scSelect.Parameters["@USERANSWER"].Value = userAnswer;

            scSelect.Parameters["@USEREMAIL"].Value = userEmail;

            scSelect.Parameters["@ACTIVATIONSTRING"].Value = activationString;

            try

            {

                regConn.Open();

                scSelect.ExecuteNonQuery();

              userID = Convert.ToInt64(scSelect.Parameters["ReturnID"].Value);

          }

            catch (Exception ex)

            {

                dsUser = null;

                throw ex;

            }

            finally

            {

                regConn.Close();

            }

            return userID;

        }

关键是粗体部分的。

偶的对程序过程的修改部分,算是另外一种实现方法

USE [VisualWar]

GO

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

Create PROCEDURE [dbo].[User_Reg]

@TRIBEID bigint,

@USERNAME nvarchar(128),

@USERPASSWORD nvarchar(128),

@USERQUESTION nvarchar(128),

@USERANSWER nvarchar(128),

@USEREMAIL nvarchar(128),

@ACTIVATIONSTRING nvarchar(128),
@UserID int output

AS

BEGIN

SET NOCOUNT ON;

INSERT INTO

U_User(

TribeID,

UserName,

UserPassword,

UserQuestion,

UserAnswer,

UserEmail,

ActivationString

)

VALUES(

@TRIBEID,

@USERNAME,

@USERPASSWORD,

@USERQUESTION,

@USERANSWER,

@USEREMAIL,

@ACTIVATIONSTRING

)
Select @UserID=@@identity

END

info Last modified by Raymond at 6 years ago
info About author

info License/Terms

Please log in or register to comment. account_circle Log in person_add Register
comment Comments (0)
No comments yet.