检查SQL表值/使用返回值作为参数

时间:2015-08-13 05:46:23

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

我对使用ASP.NET和SQL有点新,我遇到了麻烦。函数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;


    }

2 个答案:

答案 0 :(得分: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 提及。但是,如果你的状态只能是一点/布尔,你真的需要传递它吗?

答案 1 :(得分:0)

更改方法

private void ChangeFileStatus() {}

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

你可以调用类似

的方法
int Status=CheckStatus();
ChangeFileStatus(Status);

ChangeFileStatus(CheckStatus());