[英]Returning boolean value from a stored procedure using bit
This is my code for a stored procedure that checks for Email availability. 这是我用于检查电子邮件可用性的存储过程的代码。
ALTER PROCEDURE [dbo].[usp_CheckEmailMobile](@Name VARCHAR(50), @Email NVARCHAR(50), @Password NVARCHAR(50), @CountryCode INT, @Mobile VARCHAR(50), @Result BIT OUTPUT)
AS
BEGIN
IF EXISTS (SELECT COUNT (*) FROM AUser WHERE [Email] = @Email AND [Mobile] = @Mobile)
SELECT 'FALSE'; --Email &/or Mobile does not exist in database
ELSE
--Insert the record & register the user
INSERT INTO [AUser] ([Name], [Email], [Password], [CountryCode], [Mobile]) VALUES (@Name, @Email, @Password, @CountryCode, @Mobile)
END
How do I assign the result of this SP to @Result (its the output parameter)?? 如何将此SP的结果分配给@Result(其输出参数)?
here is the cs CODE: Where am I going wron in this?? 这是CS代码:我要去哪里喝?
protected void btnRegister_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]);
con.Open();
SqlCommand Cmd = new SqlCommand("usp_CheckEmailMobile", con);
Cmd.CommandType = CommandType.StoredProcedure;
Cmd.CommandText = "Registration";
Cmd.Parameters.AddWithValue("@Name", txtName.Text);
Cmd.Parameters.AddWithValue("@Email", txtEmailAddress.Text);
Cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
Cmd.Parameters.AddWithValue("@CountryCode", ddlCountryCode.Text);
Cmd.Parameters.AddWithValue("@Mobile", txtMobileNumber.Text);
//Cmd.Parameters.Add("@Result", DbType.Boolean);
SqlParameter sqlParam = new SqlParameter("@Result", DbType.Boolean);
//sqlParam.ParameterName = "@Result";
//sqlParam.DbType = DbType.Boolean;
sqlParam.Direction = ParameterDirection.Output;
Cmd.Parameters.Add(sqlParam);
Cmd.ExecuteNonQuery();
con.Close();
Response.Write(Cmd.Parameters["@Result"].Value);
}
went through this, dint help... How to run the stored procedure that has OUTPUT parameter from C#? 经历了这个,dint帮助... 如何从C#运行具有OUTPUT参数的存储过程?
You just set it as a normal variable like this 您只需将其设置为这样的普通变量
ALTER PROCEDURE [dbo].[usp_CheckEmailMobile](@Name VARCHAR(50), @Email NVARCHAR(50), @Password NVARCHAR(50), @CountryCode INT, @Mobile VARCHAR(50), @Result BIT OUTPUT)
AS
BEGIN
IF EXISTS (SELECT COUNT (*) FROM AUser WHERE [Email] = @Email AND [Mobile] = @Mobile)
BEGIN
SELECT 'FALSE'; --Email &/or Mobile does not exist in database
@Result = 0
END
ELSE
BEGIN
--Insert the record & register the user
INSERT INTO [AUser] ([Name], [Email], [Password], [CountryCode], [Mobile]) VALUES (@Name, @Email, @Password, @CountryCode, @Mobile)
@Result = 1
END
END
For server side code 对于服务器端代码
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]);
con.Open();
SqlCommand Cmd = new SqlCommand("usp_CheckEmailMobile", con);
Cmd.CommandType = CommandType.StoredProcedure;
Cmd.Parameters.AddWithValue("@Name", txtName.Text);
Cmd.Parameters.AddWithValue("@Email", txtEmailAddress.Text);
Cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
Cmd.Parameters.AddWithValue("@CountryCode", ddlCountryCode.Text);
Cmd.Parameters.AddWithValue("@Mobile", txtMobileNumber.Text);
SqlParameter sqlParam = new SqlParameter("@Result", DbType.Boolean);
sqlParam.Direction = ParameterDirection.Output;
Cmd.Parameters.Add(sqlParam);
Cmd.ExecuteNonQuery();
con.Close();
Response.Write(Cmd.Parameters["@Result"].Value);
Do this: 做这个:
ALTER PROCEDURE [dbo].[usp_CheckEmailMobile](@Name VARCHAR(50), @Email NVARCHAR(50), @Password NVARCHAR(50), @CountryCode INT, @Mobile VARCHAR(50), @Result BIT OUTPUT)
AS
BEGIN
Declare @result bit
IF EXISTS (SELECT COUNT (*) FROM AUser WHERE [Email] = @Email AND [Mobile] = @Mobile)
Begin
Set @result=0; --Email &/or Mobile does not exist in database
End
ELSE
Begin
--Insert the record & register the user
INSERT INTO [AUser] ([Name], [Email], [Password], [CountryCode], [Mobile]) VALUES (@Name, @Email, @Password, @CountryCode, @Mobile)
Set @result=1 --True
End
Select @result as Result
END
cs Code: cs代码:
bool? IsSuccess= YourDBObject.usp_CheckEmailMobile(all params).FirstOrDefault().Result;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.