2017-06-01 45 views
0

發現我寫了下面如何調用程序aspx.cs文件retrun真或假時記錄在數據庫

CREATE PROCEDURE [dbo].[CheckPI] 
    @PI Varchar(50) 

AS BEGIN 
    SET NOCOUNT ON; 

    DECLARE @Exists INT 

    IF EXISTS(SELECT * FROM Tbl_ILSM_Quotation WHERE QuotationNo = @PI) 
    BEGIN 
     SET @Exists = 1 
    END 
    ELSE 
    BEGIN 
     SET @Exists = 0 
    END 

    RETURN @Exists 
// when i execute this code in sql then it gives right ans 
    DECLARE @ReturnValue INT 
    EXEC @ReturnValue = @ReturnValue 
    SELECT @ReturnValue 
    END 

給出aspx.cs文件

protected string GetPI() 
    { 
     int customerId = GetCustomerID(); // customer id - 123 
     int year = Convert.ToInt32(ddlIdYear.SelectedValue); 
     string PI = "PI/" + year + "/" + customerId; // PI - PI/2017/123 
     //SqlDataReader myReader = null; 
     SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["JSSConnection"].ToString()); 
     con.Open(); 
     SqlCommand cmd = new SqlCommand("CheckPI", con); 
     cmd.Parameters.AddWithValue("@PI", PI); 
     cmd.CommandType = CommandType.StoredProcedure; 
     SqlParameter sqlParam = new SqlParameter("@ReturnValue", DbType.Boolean); 
     sqlParam.Direction = ParameterDirection.Output; 
     cmd.Parameters.Add(sqlParam); 
     cmd.ExecuteNonQuery(); 
     //int retrnval = Convert.ToInt32(cmd.ExecuteScalar()); 
     con.Close(); 
     //Response.Write(cmd.Parameters["@ReturnValue"].Value); 

     return PI; 

    } 
其返回1和0的程序

我做了程序來檢查pi數據庫是否可用,如果可用,則返回1,否則返回1 0 然後我在aspx.cs文件中調用該SP,但我無法檢查它在執行1或0後返回的內容

+1

1)['RETURN'](https://docs.microsoft.com/en- us/sql/t-sql/language-elements/return-transact-sql)立即退出存儲過程,之後的任何命令都將被忽略。 2)還有一個['ParameterDirection.ReturnValue'](https://msdn.microsoft.com/en-us/library/system.data.parameterdirection.aspx)。 3)'ExecuteNonQuery'就足夠了,不需要再執行'ExecuteScalar'。 4)你忽略'retrnval' –

+0

參數方向不輸出它是'ReturnValue'。但是你應該使用'Output',因爲按照慣例,返回值是成功或失敗的報告,而不是實際的數據結果。 – Crowcoder

+0

Dp你真的需要一個存儲過程來完成這個簡單的任務嗎? – Steve

回答

1
// Add an Out param to capture the return value from the Procedure 
SqlParameter outParam = new SqlParameter(); 
outParam.SqlDbType = System.Data.SqlDbType.Int; 
outParam.ParameterName = 「@outParam」; 
outParam.Direction = System.Data.ParameterDirection.Output; 
cmd.Parameters.Add(outParam); 

// Add an Out param to capture whether the Stored Proc executed correctly or not (exception) 
SqlParameter retParam = new SqlParameter(); 
retParam.SqlDbType = System.Data.SqlDbType.Int; 
retParam.ParameterName = 「@retParam」; 
retParam.Direction = System.Data.ParameterDirection.ReturnValue; 
cmd.Parameters.Add(retParam); 

// Execute the command 
cmd.ExecuteNonQuery(); 

// Get the values 
int retval = (int)cmd.Parameters[「@retParam」].Value; 
int outval = (int)cmd.Parameters[「@outParam」].Value; // Should contain the value you've returned for existence of PI value 

您已經使用INT表示布爾值,您可以將其更改爲存儲過程中的BIT以保持一致。

+0

當我添加int retval =(int)cmd.Parameters [「@ retParam」]。那麼它會給出語法錯誤 –

+0

請添加錯誤的詳細信息。可能是值返回null,並且類型轉換拋出異常 – Gururaj

+0

當我在cmd.CommandType = CommandType.StoredProcedure; ===之後添加代碼時,添加一個Out參數以捕獲它提供的過程的返回值過程或函數CheckPI指定的參數太多。錯誤 –

0

相反,這些行:

 SqlParameter sqlParam = new SqlParameter("@ReturnValue", DbType.Boolean); 
    sqlParam.Direction = ParameterDirection.Output; 

使用這樣的:

 SqlParameter sqlParam = new SqlParameter("@ReturnValue", DbType.Int32); 
    sqlParam.Direction = ParameterDirection.ReturnValue; 

的值從存儲過程返回(與RETURN語句)始終是一個整數,而不是一個布爾型(或串)。所以參數的類型需要改變。加上你需要返回值,因爲你沒有聲明任何輸出參數,所以你需要一個不同的方向。

,然後你執行與

cmd.ExecuteNonQuery(); 

查詢沒有返回值(一切都在你的存儲過程返回的是忽略後)SELECT語句,所以這是不夠的。
執行後,可以檢查返回值:

int retVal = Convert.ToInt32(cmd.Parameters["@ReturnValue"].Value); 

然後你就可以使用該retVal的變量,例如通過使用return retVal == 1;。但是,您需要將方法的返回類型從string更改爲bool

0

我解決了我的問題。和回答如下

給定存儲過程

CREATE PROCEDURE [dbo].[CheckPI] 
@PI Varchar(50), 
@Exists INT = 0 out 

AS BEGIN 
SET NOCOUNT ON; 
IF EXISTS(SELECT * FROM Tbl_ILSM_Quotation WHERE QuotationNo = @PI) 
BEGIN 
    SET @Exists = 1 
END 
ELSE 
BEGIN 
    SET @Exists = 0 
END 

和aspx.cs文件代碼

protected string GetPI() 
    { 
     int customerId = GetCustomerID(); // customer id - 123 
     int year = Convert.ToInt32(ddlIdYear.SelectedValue); 
     string PI = "PI/" + year + "/" + customerId; 
     SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["JSSConnection"].ToString()); 
     con.Open(); 
     for (char i = 'A'; i <= 'Z'; i++) 
     { 
      PI = "PI/" + year + "/" + customerId + i; // PI - PI/2017/123 
      //SqlDataReader myReader = null; 

      SqlCommand cmd = new SqlCommand("CheckPI", con); 
      cmd.CommandType = CommandType.StoredProcedure; 
      cmd.Parameters.AddWithValue("@PI", PI); 
      SqlParameter outputParameter = new SqlParameter(); 
      outputParameter.ParameterName = "@Exists"; 
      outputParameter.SqlDbType = System.Data.SqlDbType.Int; 
      outputParameter.Direction = System.Data.ParameterDirection.Output; 
      cmd.Parameters.Add(outputParameter); 
      cmd.ExecuteNonQuery(); 
      int returnVal = Convert.ToInt32(outputParameter.Value.ToString()); 

      if (returnVal == 1) 
      { 
       continue; 

      } 
      else 
      { 
       break; 
      } 
     } 
     //else 
     //{ 

     // PI = "PI/" + year + "/" + customerId; 
     //} 

     con.Close(); 
     //Response.Write(cmd.Parameters["@ReturnValue"].Value); 

     return PI; 

    } 
相關問題