我需要将一个参数传递给存储过程以获取列数据类型为Bit的所有行。 例如:
select * from user where active = true // get all actived users
select * from user where active = false // get all Not actived users
当我需要获取所有行时,我该怎么办?我将活动值作为参数传递给C#
答案 0 :(得分:2)
您可以将active参数设为可选参数,并执行以下操作:
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
答案 1 :(得分:0)
取决于活动列中的数据。 如果你有真假,我怀疑,因为sql db不支持这些值tyes,但你至少可以有位值类型。所以位只能有1或0(一或零)。
答案 2 :(得分:0)
使用?
取消您的C#bool
值:
bool? showActive = SomeCode();
传递可能为空的showActive
标志,并使用以下命令更新SQL代码:
select * from user where active = @showActive or @showActive is null
如果传递的@showActive
参数为null,则返回所有行。
答案 3 :(得分:0)
这也可以像:
那样实现WHERE user.active = ISNULL(@Active, user.active)
使用它,SQL服务器可以使用索引(如果有索引可以使用)。
答案 4 :(得分: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 ) ;