2013-03-22 55 views
0

這是我用於檢查電子郵件可用性的存儲過程的代碼。使用位從存儲過程返回布爾值

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

    } 

通過這個,力幫助去... How to run the stored procedure that has OUTPUT parameter from C#?

+0

這將是有用的:[如何從存儲過程返回布爾](http://stackoverflow.com/questions/3825620/how-to-return- bool-from-stored-proc) – Habib 2013-03-22 10:39:55

+0

通過這個已經存在,DINT幫助... – adityawho 2013-03-22 10:46:30

+0

嘗試使用此方法獲取值 Cmd.ExecuteNonQuery(); con.Close(); cmd.Parameters [「@ Result」]。Value; – 2013-03-22 11:19:02

回答

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

請檢查我的CS ... – adityawho 2013-03-22 11:06:24

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; 
+0

你能幫我用CS代碼嗎? – adityawho 2013-03-22 10:58:21

+0

檢查更新的答案。 – 2013-03-22 11:01:01

+0

請檢查我的CS代碼..... – adityawho 2013-03-22 11:04:56