1

これは、電子メールの可用性をチェックするストアド プロシージャのコードです。

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 (出力パラメーター) に割り当てるにはどうすればよいですか??

これが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);

    }

これを通り抜けました、dint help ... C#からOUTPUTパラメータを持つストアドプロシージャを実行するにはどうすればよいですか?

4

2 に答える 2

2

このように通常の変数として設定するだけです

    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

サーバー側コードの場合

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);
于 2013-03-22T10:48:45.183 に答える
1

これを行う:

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 コード:

bool? IsSuccess= YourDBObject.usp_CheckEmailMobile(all params).FirstOrDefault().Result;
于 2013-03-22T10:47:18.447 に答える