2011-09-07 35 views
0

我需要傳遞一個參數到存儲過程以獲取列的數據類型爲Bit的所有行。 示例:獲取數據類型的所有行的列位

select * from user where active = true // get all actived users 
select * from user where active = false // get all Not actived users 

當我需要獲取所有行時,我該怎麼辦?我將活動值作爲參數傳遞給C#

回答

0

使用?來抵消你的C# bool value:

bool? showActive = SomeCode(); 

傳遞潛在的空showActive標誌,並在此更新您的SQL代碼:

select * from user where active = @showActive or @showActive is null 

如果傳遞的@showActive參數爲空,所有行會退還。

0

取決於活動列中的數據。 如果你有true和false,我懷疑,因爲sql db不支持這種類型的值,但你至少可以有位值類型。所以位可以只有1或0(一個或零)。

2

您可以激活的參數可選的,做這樣的事情:

ALTER PROCEDURE [dbo].[GetUsers](
    @Active BIT = NULL 
) AS 

BEGIN 

    SELECT * 
    FROM user 
    WHERE (@Active IS NULL OR active = @Active) 

END 

並在代碼,添加一個重載的獲取方法:

var users = GetUsers(true); //pass active as true 

var users = GetUsers(); //dont pass active parameter, return all users 
+0

使用這個答案,並指定'null'作爲參數值如果你想獲得所有的行。 – Peter

0

這也等來實現:

WHERE user.active = ISNULL(@Active, user.active) 

用這個,如果有一個可用,並且可以使用SQL服務器可以使用索引。

+0

你試過這個嗎? SQL Server似乎只使用一個索引,其中列= ISNULL(@var,常量)',而不是'column = ISNULL(@var,column)'。 – zinglon

0

我喜歡這樣的事情。由於存儲過程:

create procedure dbo.SelectAccounts 

    @fExpired bit 

as 

    set ansi_nulls    on 
    set concat_null_yields_null on 

    select * 
    from dbo.Account acct 
    where acct.Expired = @fExpired = coalesce(@fExpired , acct.Expired) 
    -- a more verbose alternative test 
    -- (   acct.Expired = @fExpired 
    --  OR ( acct.Expired is null 
    --   and @fExpired is not null 
    --  ) 
    -- ) 

    return 0 

go 

我產生的一類,看起來是這樣的:

public class dbo_SelectAccounts 
{ 
    public const string STORED_PROCEDURE_NAME = @"dbo.SelectAccounts"; 

    private string ConnectString { get ;   set ; } 
    public int  ReturnCode  { get ; private set ; } 
    public int  TimeoutInSeconds { get ; private set ; } 
    public DataTable ResultSet  { get ; private set ; } 
    public int  RowCount   { get { return this.ResultSet.Rows.Count ; } } 

    public int Exec(bool? @fExpired) 
    { 
    using (SqlConnection conn = new SqlConnection(this.ConnectString)) 
    using (SqlCommand  cmd = conn.CreateCommand()) 
    using (SqlDataAdapter sda = new SqlDataAdapter(cmd)) 
    { 

     cmd.CommandText = STORED_PROCEDURE_NAME; 
     cmd.CommandType = CommandType.StoredProcedure; 
     cmd.CommandTimeout = this.TimeoutInSeconds ; 

     // 1. Format parameter to stored procedure 
     SqlParameter p1 = new SqlParameter(@"@fExpired" , SqlDbType.Bit) ; 
     if (@fExpired == null) 
     { 
     p1.Value = System.DBNull.Value ; 
     } 
     else 
     { 
     p1.Value = @fExpired ; 
     } 
     cmd.Parameters.Add(p1); 

     // add return code parameter 
     SqlParameter pReturnCode = new SqlParameter(); 
     pReturnCode.SqlDbType = System.Data.SqlDbType.Int; 
     pReturnCode.Direction = System.Data.ParameterDirection.ReturnValue; 
     cmd.Parameters.Add(pReturnCode); 

     conn.Open(); 
     sda.Fill(this.ResultSet) ; 
     conn.Close(); 

     this.ReturnCode = (int)pReturnCode.Value; 

    } 

    return this.ReturnCode; 
    } 

    #region constructors 

    public dbo_SelectAccounts(string connectionString , int executionTimeoutInSeconds) 
    { 
    this.ConnectString = connectionString   ; 
    this.TimeoutInSeconds = executionTimeoutInSeconds ; 
    this.ReturnCode  = -1      ; 
    this.ResultSet  = new DataTable()   ; 
    return ; 
    } 

    public dbo_SelectAccounts(string connectionString) 
    : this(connectionString , 30) 
    { 
    this.ConnectString = connectionString; 
    } 

    public dbo_SelectAccounts(SqlConnectionStringBuilder csb , int executionTimeoutInSeconds) 
    : this(csb.ConnectionString , executionTimeoutInSeconds) 
    { 
    return; 
    } 

    public dbo_SelectAccounts(SqlConnectionStringBuilder csb) 
    : this(csb.ConnectionString) 
    { 
    return; 
    } 

    #endregion constructors 

} 

用法很簡單:

dbo_SelectAccount sp = new dbo_SelectAccounts(myConnectString) ; 
int    rc = sp.Exec(null) ;