【问题标题】:How do I check whether a value is 1 or 0 in SQL?如何检查 SQL 中的值是 1 还是 0?
【发布时间】:2018-07-25 08:29:43
【问题描述】:

所以我已经设置了我的数据库连接并且它确实连接了。 现在我想检查表中的每一行 isactivated 列是 0 还是 1 因为它是一个,但我不知道该怎么做。

我会执行查询吗?我会将所有行存储在一个列表中然后检查吗?

using (var connection = new SqlConnection(cb.ConnectionString))
{
    try
    {
        connection.Open();
        if (connection.State == ConnectionState.Open)
        {
            Console.WriteLine("Connected!");
        }
        else
        {
            Console.WriteLine("Failed..");
        }
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
        throw;
    }
}

【问题讨论】:

  • 检查你的行,然后用它们做什么
  • @TheGeneral I want to check each row in my table wether the column isactivated is either 0 or 1 because it's a bit
  • 哪个没有回答我的问题,你想统计它们吗,你想过滤我的这个字段,你想只检查是否存在
  • 这是迄今为止任何初学者教程都可以回答的最初学者的问题。没有人天生就具备您需要学习的所有知识。

标签: c# mysql sql .net


【解决方案1】:
SqlCommand command = new SqlCommand("SELECT column FROM table", connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
    // 0 = false, 1 = true,
    bool result = (bool)reader[0];

    //... do whatever you wanna do with the result
}

【讨论】:

    【解决方案2】:

    我认为您正在寻找这样的东西:

    SqlConnection sqlConnection1 = new SqlConnection("Your Connection String");
    SqlCommand cmd = new SqlCommand();
    SqlDataReader reader;
    
    cmd.CommandText = "SELECT * FROM Customers";
    cmd.CommandType = CommandType.Text;
    cmd.Connection = sqlConnection1;
    
    sqlConnection1.Open();
    
    reader = cmd.ExecuteReader();
    // Data is accessible through the DataReader object here.
    
    sqlConnection1.Close();
    

    取自msdn-lib

    SqlDataReader 中应该包含您的所有数据,包括您的 isactivate 列,然后您可以检查其各自的值。

    【讨论】:

      【解决方案3】:

      如果您想读取该字段,您可以实现以下内容:

        using (var connection = new SqlConnection(cb.ConnectionString)) {
          try {
            connection.Open();
      
            // connection.Open() either succeeds (and so we print the message) 
            // or throw an exception; no need to check 
            Console.WriteLine("Connected!");
      
            //TODO: edit the SQL if required
            string sql =
              @"select IsActivated
                  from MyTable";
      
            using (var query = new SqlCommand(sql, connection)) {
              using (var reader = query.ExecuteReader()) {
                while (reader.Read()) {
                  // Now it's time to answer your question: let's read the value 
                  //   reader["IsActivated"] - value of IsActivated as an Object
                  //   Convert.ToBoolean     - let .Net do all low level work for you
                  bool isActivated = Convert.ToBoolean(reader["IsActivated"]);
      
                  // Uncomment, if you insist on 0, 1
                  // int bit = Convert.ToInt32(reader["IsActivated"]);
      
                  //TODO: field has been read into isActivated; put relevant code here  
                }
              }
            }
          }
          catch (DataException e) { // Be specific: we know how to process data errors only
            Console.WriteLine(e);
      
            throw;
          }
        }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-09-15
        • 1970-01-01
        • 2010-09-12
        • 2021-03-09
        • 2021-07-02
        • 2018-10-03
        • 2021-10-19
        相关资源
        最近更新 更多