【发布时间】: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