如何将Bool(BIT)参数传递给SQL服务器?

时间:2021-12-16 10:26:35

I have a stored procedure in sql which has a bit parameter.I need to pass the parameter value from c#.Can Someone help me how to do it. I passed true/false value to the bit parameter but it is not working.

我在sql中有一个存储过程,它有一个bit参数。我需要从c#传递参数值。有人帮我怎么做。我将true / false值传递给bit参数但它不起作用。

CREATE PROCEDURE checkbit
      @id varchar(10),
@IsDeleted bit
AS
BEGIN

IF(@IsDeleted = 1)
    BEGIN
        DELETE FROM tablename WHERE ID = @id

        RETURN
    END
END

My C# code

我的C#代码

Im using entity framework.Checking for bit value here.

我正在使用实体框架。在这里检查比特值。

bool chk;
if(val==1)
 chk=true;
else
 chk=false;
context.checkbit(id,chk)  

2 个解决方案

#1


11  

You can just use 0 or 1 for the BIT field

您可以在BIT字段中使用0或1

  • 1 for TRUE
  • 1为TRUE

  • 0 for FALSE
  • 0表示FALSE


context.checkbit(id,val);// ? 1 : 0;

#2


2  

IF your using Ado.net then try with below hope it will work.

如果您使用Ado.net然后尝试以下希望它将工作。

SqlParameter param = new SqlParameter();
             param.ParameterName = "@Isstatus";
             param.Value = Isstatus;
             param.DbType = System.Data.DbType.Boolean
             cmd.Parameters.Add(param);

Entity Framework

if your sp return true or false then below you can use other wise you need to try with void.

如果你的sp返回true或false,那么你可以使用其他明智的你需要尝试使用void。

using System.Data.Entity.Infrastructure;
using System.Data.Objects;

    public virtual bool Deletecustomer(int id ,bool IsDeleted )
    {
        return ((IObjectContextAdapter)this).ObjectContext.ExecuteStoreQuery<bool>("EXEC checkbit({0},{1})", id,IsDeleted ).SingleOrDefault();
    }

#1


11  

You can just use 0 or 1 for the BIT field

您可以在BIT字段中使用0或1

  • 1 for TRUE
  • 1为TRUE

  • 0 for FALSE
  • 0表示FALSE


context.checkbit(id,val);// ? 1 : 0;

#2


2  

IF your using Ado.net then try with below hope it will work.

如果您使用Ado.net然后尝试以下希望它将工作。

SqlParameter param = new SqlParameter();
             param.ParameterName = "@Isstatus";
             param.Value = Isstatus;
             param.DbType = System.Data.DbType.Boolean
             cmd.Parameters.Add(param);

Entity Framework

if your sp return true or false then below you can use other wise you need to try with void.

如果你的sp返回true或false,那么你可以使用其他明智的你需要尝试使用void。

using System.Data.Entity.Infrastructure;
using System.Data.Objects;

    public virtual bool Deletecustomer(int id ,bool IsDeleted )
    {
        return ((IObjectContextAdapter)this).ObjectContext.ExecuteStoreQuery<bool>("EXEC checkbit({0},{1})", id,IsDeleted ).SingleOrDefault();
    }