2011-09-07 44 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) ;