【问题标题】:Checking SQL Table Value / Using return value as parameter检查 SQL 表值/使用返回值作为参数
【发布时间】:2015-08-13 05:46:22
【问题描述】:

我对使用带有 SQL 的 ASP.NET 有点陌生,但我遇到了麻烦。函数 CheckStatus() 基本上调用一个存储过程来检查一个表列值,该值只会是 1 或 0。表中的数据类型有点,所以我的想法是将其转换为字符串并从那里。之后,它返回 1 或 0。

第二个函数 ChangeFileStatus() 应该将值更改为 1 或 0,具体取决于它是什么。

但是,我的问题是我可以在另一个函数中使用我的返回值作为参数吗?我想对返回值进行 if 条件检查。请帮忙。

private void ChangeFileStatus()
{
    CheckStatus(); // i wanna call this here but use return value as the paramter


    using (SqlConnection con = new SqlConnection(CS))
    {
        cmd = new SqlCommand("spEcovaFilesChangeJobStats", con); //call your stored procedure within the ""
        cmd.CommandType = CommandType.StoredProcedure; // this is saying that the command type is a stored procedure
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();

    }



}

private int CheckStatus()
{
    int status = 0;

    using (SqlConnection con = new SqlConnection(CS))
    {
        cmd = new SqlCommand("spEcovaGetFilesJobStats", con); //call your stored procedure within the ""
        cmd.CommandType = CommandType.StoredProcedure; // this is saying that the command type is a stored procedure
        con.Open();
        cmd.ExecuteNonQuery();


        SqlDataReader rdr = cmd.ExecuteReader();
        if (rdr.HasRows)
        {
            while (rdr.Read())
            {

               string active = rdr["IsActive"].ToString();

                if (active == "1" )
                {
                    status = 0 ;

                }

                else
                {
                    status = 1;
                }


            }
        }

        con.Close();

        return status;


    }

【问题讨论】:

  • 你的意思是你想检查像if checkstatus()=='1' ??
  • 检查你的逻辑。在这两种情况下,状态 = 0。如果你只是得到一个返回值类型位(所以 0 或 1(或者可能是 DBNull)),你可以使用: bool isActive=reader["IsActive"] as bool? ??假的;
  • @Tjasun 感谢您发现我刚刚修复了它
  • @akhilkumar 是的,这正是我想要的,但我对 ExecuteReader() 函数也有点困惑。

标签: c# sql asp.net .net database


【解决方案1】:

所以我认为您可能需要重新考虑您的应用程序的几件事。您说您的表存储了一点,为什么不使用 C# 等效的 bool?无需转换。另外关于您对存储过程的调用,您是否期望超过 1 个值?如果不是,为什么不做以下事情来检查状态,这为用户提供了一个清晰的画面,而不是一个你必须弄清楚/记住的幻数

private bool CheckStatus()
{
    bool wasSuccessful = false;

    using (SqlConnection con = new SqlConnection(CS))
    {
        cmd = new SqlCommand("spEcovaGetFilesJobStats", con);
        cmd.CommandType = CommandType.StoredProcedure;
        con.Open();
        wasSuccessful = (bool)cmd.ExecuteScalar();
        con.Close();
    }
    return status;
}

如果您希望它进入更改状态,您只需更改 ChangeFileStatus 的签名,例如 learningNew 提及。但是,如果您的状态只能是位/布尔值,您真的需要通过它吗?

【讨论】:

    【解决方案2】:

    改变方法

    private void ChangeFileStatus() {}
    

    private void ChangeFileStatus(int Status)
    {
    //check staus value here
    }
    

    你可以调用类似的方法

    int Status=CheckStatus();
    ChangeFileStatus(Status);
    

    ChangeFileStatus(CheckStatus());
    

    【讨论】:

      猜你喜欢
      • 2011-04-06
      • 2011-08-30
      • 2014-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-19
      • 2019-04-23
      • 2015-04-11
      相关资源
      最近更新 更多