使用比特存储过程返回布尔值 [英] Returning boolean value from a stored procedure using bit

查看:177
本文介绍了使用比特存储过程返回布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的code为检查电子邮件可用性的存储过程。

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

我如何分配该SP的结果@Result(它的输出参数)??

How do I assign the result of this SP to @Result (its the output parameter)??

这里是CS code:我要去哪里这个wron ??

here is the cs CODE: Where am I going wron in this??

      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);

    }

通过这个,力帮助去...
<一href=\"http://stackoverflow.com/questions/3433694/how-to-run-the-stored-procedure-that-has-output-parameter-from-c\">How要在带有OUTPUT参数从C#存储过程?

推荐答案

您只需将它设置为这样一个正常的变量

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

有关服务器端code

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);

这篇关于使用比特存储过程返回布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆