如何根据从查询中检索到的值制作“ if”?

时间:2019-04-16 07:41:13

标签: c# sql

我需要在表中插入“通过”或“失败”,如果从查询中发现只有一个失败而不是“ RProva”,那么如果查询找不到单个失败,我就必须输入“失败”代替“ RProva”,我必须插入“ pass”,查询正在运行,但是我不知道如何对查询结果进行处理,也许我必须使用“ for”?不知道这是尝试,第二次是查询,我必须在其中插入可能的“ IF”结果

1。

    SqlCommand cmdRD = new SqlCommand("SELECT ResItem AS RD FROM tSE JOIN tL ON tSE.idSE=tL.idL WHERE tL.Selection=1");
    var RD = cmdRD.ExecuteScalar();

    var values = new List<string>();
    using (cmdRD,sqliteCon)
    {
        using (SqlDataReader reader = cmdRD.ExecuteReader())
         {
            while (reader.Read())
            {
                values.Add(reader[0].ToString());
            }
        }
    }

2。

    SqlCommand cmd1 = new SqlCommand("INSERT INTO tSD(NomeItem,ResItemDet,DateStartDet,DateEndDet) OUTPUT inserted.Id VALUES (@NI,@RProva,@DATESE,@DATEED)");
    cmd1.Parameters.AddWithValue("@DATESE", DATESE);
    cmd1.Parameters.AddWithValue("@DATEED", DATEED);
    cmd1.Parameters.AddWithValue("@NI", NI);


    using (cmd1,sqliteCon)
    {
          foreach (var value in values)
          {
             if (value.Equals(pass))
             {
                        cmd1.Parameters.AddWithValue("@RProva", value);
             }
             else
             {
                        cmd1.Parameters.AddWithValue("@RProva", fail);
             }
                    cmd1.ExecuteNonQuery();
          }
    }

    int generatedId = Convert.ToInt32(cmd1.ExecuteScalar());
    cmd1.Parameters.Clear();


    SqlCommand cmd2 = new SqlCommand("UPDATE tSE SET FK_TSD_id = @tsdId FROM tL JOIN tSE ON tL.idL = tSE.idSE WHERE tL.Selection=1 ", sqliteCon);
    cmd2.Parameters.AddWithValue("@tsdId", generatedId);
    cmd2.ExecuteNonQuery();

    MessageBox.Show("Dato Aggiunto");

  }
  sqliteCon.Close();

1.1

    SqlCommand cmdRD = new SqlCommand("SELECT ResItem AS RD FROM tSE JOIN tL ON tSE.idSE=tL.idL WHERE tL.Selection=1", sqliteCon);
    var RD = cmdRD.ExecuteScalar();

    var tot =pass;
    using (cmdRD)
    {
        using (SqlDataReader reader = cmdRD.ExecuteReader())
         {
            while (reader.Read())
            {
                if(reader[0].ToString()==fail)
                {
                            tot = fail;
                            break;
                }
            }
            MessageBox.Show(tot);
        }
    }

我到达了(1.1)可以正常工作的地方,但是我必须将TOT插入RProva

1 个答案:

答案 0 :(得分:1)

根据您的问题和评论,我认为您误解了ExecuteScalar:
ExecuteScalar执行查询,并返回查询返回的结果集中第一行的第一列。 docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlcommand.executescalar

我不知道“ RD”或“ PASS”的类型,但是示例中的IF(使用ExecuteScalar时)似乎还可以。否则,当您使用ExecuteReader时,应使用某种循环来遍历Items。像这样:

string pass = "abc"; // guessing types and values
string fail = "failed";
string sqliteCon = "Data Source=(localdb)\\MSSQLLocalDB;Database=BooksDb";

using (SqlConnection connection = new SqlConnection(sqliteCon))
{
    connection.Open();

    var queryString = @"SELECT ResItem AS RD 
                FROM tSE 
                JOIN tL ON tSE.idSE = tL.idL 
                WHERE tL.Selection=1";

    var values = new List<string>();
    using (SqlCommand command = new SqlCommand(queryString, connection))
    {
        using (SqlDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                values.Add(reader[0].ToString());
            }
        }
    }

    var queryInsert = @"INSERT INTO tSD (NomeItem,ResItemDet,DateStartDet,DateEndDet) 
                                VALUES (@NI, @RProva, @DATESE, @DATEED)";
    using (SqlCommand command2 = new SqlCommand(queryInsert, connection))
    {
        foreach(var value in values)
        {
            command2.Parameters.Clear();
            if (value.Equals(pass))
            {
                command2.Parameters.AddWithValue("@RProva", value);
            }
            else
            {
                command2.Parameters.AddWithValue("@RProva", fail);
            }
            command2.ExecuteNonQuery();
        }
    }
}

肯定有更好的方法,但这也许可以帮到您。

编辑: 这是我如何实现您的代码示例:

using (SqlConnection sqliteCon = new SqlConnection(connection))
{
    sqliteCon.Open();
    var values = new List<string>();
    var query = "SELECT ResItem AS RD FROM tSE JOIN tL ON tSE.idSE=tL.idL WHERE tL.Selection=1";
    using (SqlCommand cmdRD = new SqlCommand(query, sqliteCon))
    {
        var RD = cmdRD.ExecuteScalar();
        using (SqlDataReader reader = cmdRD.ExecuteReader())
        {
            while (reader.Read())
            {
                values.Add(reader[0].ToString());
            }
        }
    }

    int generatedId = 0;
    var query2 = "INSERT INTO tSD(NomeItem,ResItemDet,DateStartDet,DateEndDet) OUTPUT inserted.Id VALUES (@NI,@RProva,@DATESE,@DATEED)";
    using (SqlCommand cmd1 = new SqlCommand(query2, sqliteCon))
    {
        foreach (var value in values)
        {
            cmd1.Parameters.Clear();
            cmd1.Parameters.AddWithValue("@DATESE", DATESE);
            cmd1.Parameters.AddWithValue("@DATEED", DATEED);
            cmd1.Parameters.AddWithValue("@NI", NI);

            if (value.Equals(pass))
            {
                cmd1.Parameters.AddWithValue("@RProva", value);
            }
            else
            {
                cmd1.Parameters.AddWithValue("@RProva", fail);
            }
            cmd1.ExecuteNonQuery();
        }

        generatedId = Convert.ToInt32(cmd1.ExecuteScalar());
    }

    var query3 = "UPDATE tSE SET FK_TSD_id = @tsdId FROM tL JOIN tSE ON tL.idL = tSE.idSE WHERE tL.Selection=1 ";
    using (SqlCommand cmd2 = new SqlCommand(query3, sqliteCon))
    {
        cmd2.Parameters.AddWithValue("@tsdId", generatedId);
        cmd2.ExecuteNonQuery();
    }
}