我正在创建新的用户注册模块并为此编写了存储过程。
PROCEDURE [dbo].[addNewUser]
-- Add the parameters for the stored procedure here
@usertype VarChar(10),
@useremail VarChar(70),
@userpass VarChar(20),
@fullname VarChar(70),
@city VarChar(70),
@state Int,
@allowAlerts Bit,
@allowLetter Bit,
@aboutMe NVARCHAR(160)
As
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
if ((select count(user_info._id) from user_info where useremail like @useremail) = 0)
BEGIN
Insert Into user_info
(usertype,useremail,userpass,fullname,city,[state],allowAlerts,allowLetters,aboutMe)
Values
(
@usertype,
@useremail,
@userpass ,
@fullname,
@city,
@state,
@allowAlerts,
@allowLetter,
@aboutMe
)
Select @@IDENTITY as NewID
END
Else
BEGIN
Print '-1'
END
下面是我尝试使用的简单 ASP.net C# 代码
public int registerNewUser(string usertype, string useremail, string userpass, string fullname, string city, string state, string allowAlerts, string allowLetter, string aboutMe)
{
con = new SqlConnection(connectionString);
cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "addBlogEntry";
cmd.Parameters.Add("@usertype", SqlDbType.VarChar).Value = usertype;
cmd.Parameters.Add("@useremail", SqlDbType.VarChar).Value = useremail;
cmd.Parameters.Add("@userpass", SqlDbType.VarChar).Value = userpass;
cmd.Parameters.Add("@fullname", SqlDbType.VarChar).Value = fullname;
cmd.Parameters.Add("@city", SqlDbType.VarChar).Value = city;
cmd.Parameters.Add("@state", SqlDbType.VarChar).Value = Convert.ToInt16(state);
cmd.Parameters.Add("@allowAlerts", SqlDbType.VarChar).Value = Convert.ToInt16(allowAlerts);
cmd.Parameters.Add("@allowLetter", SqlDbType.VarChar).Value = Convert.ToInt16(allowLetter);
cmd.Parameters.Add("@aboutMe", SqlDbType.VarChar).Value = aboutMe;
try
{
if (con.State != ConnectionState.Open)
con.Open();
cmd.ExecuteNonQuery();
con.Close();
con.Dispose();
// some code to be written here so that i can return userID(success) or -1(if that username is already registered)
return 0;
}
catch (Exception Ex)
{
con.Close();
con.Dispose();
return 0;
}
}
通过我的 C# 代码,我想返回我的存储过程返回给我的自动生成的 userId,或者如果用户已经存在,我想返回 -1
请告诉我怎么做?
提前致谢:)
请您参考如下方法:
是的,您可以使用 ExecuteScalar() 并更改
Print '-1'
进入
Select -1 as NewID
