如果条件在里面使用sql语句

时间:2017-11-22 08:54:54

标签: c# sql asp.net if-statement

int stageID=0;
if (stageID != 1 && stageID != 3 && stageID != 4 && stageID != 5)
{
    StageTwo();
}

我想从数据库中获取stageId的值,并在if语句的条件下显示它。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

如果是c#代码,那么您可以使用contains来检查stageID

if(stageID.Contains(1))
{
//your code
}
else if(stageID.Contains(2))
{
//your code
}
else
{
// your code
}

如果是SQL代码

DECLARE 
   stageID number(3) := 100; 
BEGIN 
   -- check the boolean condition using if statement  
   IF( stageID < 20 ) THEN 
      -- if condition is true then print the following   
      dbms_output.put_line('stageID is less than 20 ' ); 
   ELSE 
      dbms_output.put_line('stageID is not less than 20 ' ); 
   END IF; 
   dbms_output.put_line('value of stageID is : ' || stageID); 
END; 

从c#中获取stageID中的数据

try{
    int stageID  = 0;
    string strConnect = @"Database=Database_Name;Data Source=Your_Server_Name;Initial Catalog=Your_Db_Name;Integrated Security=True";
    using (SqlConnection con = new SqlConnection(strConnect))
    {
        con.Open();
        con.InfoMessage += new SqlInfoMessageEventHandler(con_InfoMessage);
    }
    SqlConnection Conn = new SqlConnection(Connection_String);
    SqlCommand Comm1 = new SqlCommand(Command, Conn);
    Conn.Open();
    SqlDataReader DR1 = Comm1.ExecuteReader();
    if (DR1.Read())
    {
        stageID   = Convert.ToInt32(DR1.GetValue(0));
    }
    Conn.Close();
}
catch(exception ex)
{
// your exceptio code
}

让我知道更多帮助