Sql Reader Loop包含Select,用于确定要插入的数据

时间:2014-08-21 02:56:34

标签: c# sql sql-server datatable dataadapter

您好我是C#/ ADO的新手我编写了以下代码。我本质上想要打开数据库并使用select语句创建一个阅读器。然后我想循环读取该读取器并获取一个列值,以便在第二个sql语句中使用。如果ExecuteScaler不为null并且我知道只有一条记录,那么我想将一条记录插入到Spindletime作为最后一个parm的数据集中。如果它为null,那么我想插入相同的记录,但最后一个parm为零。我想做这个检查并插入阅读器中的每一行。然后关闭商店。有关更好的方法的提示将有所帮助。我可以使用dataadapter.fill吗?

这是我的代码

conn.Open

using (SqlCommand cmd1 = new SqlCommand("Select Distinct PartName, OrderName, RequiredQuantity, StartDate, CompleteDate, NormalQuantity, ScrapQuantity, OrderComment from OrderResults"))
{
    using (SqlDataReader 1streader = cmd1.ExecuteReader())
    {
        while (1streader.Read())
        {
            string strordername = reader.GetString(2);  get the ordername

            using (SqlCommand cmd2 = new SqlCommand("Select OrderName,
            Sum(SpindleTime) as TotalSpindle Time from MachineResults 
            Where @OrderName = strordername"))
      {
            cmd2.CommmandType = CommandType.Text;
                cmd2.Parameters.AddWithValues(“@OrderName”, strordername)

                object o = cmd2.ExecuteScalar(); 
                if(o != null)  Check if record exists
                {

        SqlCommand cmd3 = new SqlCommand();
        cmd3.Connection = conn;
  cmd3.CommandText = "INSERT INTO PartResults(PartName,
      OrderName, RequiredQuantity, StartDate, CompleteDate, 
      NormalQuantity, ScrapQuantity, OrderComment,
      SpindleTime)
                         VALUES
                           (@param1, @param2, @param3, @param4, @param5, @param6, 
                            @param7, @param8, @param9)";

        cmd3.Parameters.AddWithValue("@param1", 
        reader.GetString(1));
        cmd3.Parameters.AddWithValue("@param2", 
         reader.GetString(2));
        cmd3.Parameters.AddWithValue("@param3", 
        reader.GetString(3));
        cmd3.Parameters.AddWithValue("@param4", 
        reader.GetString(4));
        cmd3.Parameters.AddWithValue("@param5", 
        reader.GetString(5));
        cmd3.Parameters.AddWithValue("@param6", 
        reader.GetString(6));
        cmd3.Parameters.AddWithValue("@param7",
        reader.GetString(7));
        cmd3.Parameters.AddWithValue("@param8", 
        reader.GetString(8));
        cmd3.Parameters.AddWithValue("@param9", TotalSpindle));

                cmd3.ExecuteNonQuery(); 
                   }
                    else 
                    {
          SqlCommand cmd4 = new SqlCommand();
         cmd4.Connection = conn;
   cmd4.CommandText = "INSERT INTO PartResults(PartName,
      OrderName, RequiredQuantity, StartDate, CompleteDate, 
      NormalQuantity, ScrapQuantity, OrderComment,
      SpindleTime)
                         VALUES
                           (@param1, @param2, @param3, @param4, @param5, @param6, 
                            @param7, @param8, @param9)";

        cmd4.Parameters.AddWithValue("@param1", 
        reader.GetString(1));
        cmd4.Parameters.AddWithValue("@param2", 
        reader.GetString(2));
        cmd4.Parameters.AddWithValue("@param3", 
        reader.GetString(3));
        cmd4.Parameters.AddWithValue("@param4",
        reader.GetString(4));
        cmd4.Parameters.AddWithValue("@param5", 
        reader.GetString(5));
        cmd4.Parameters.AddWithValue("@param6", 
        reader.GetString(6));
        cmd.Parameters.AddWithValue("@param7",
        reader.GetString(7));
        cmd4.Parameters.AddWithValue("@param8", 
        reader.GetString(8));
        cmd4.Parameters.AddWithValue("@param9", 0));

                cmd4.ExecuteNonQuery(); 
                  }
               }  

                }
        }
        1streader.Close();
    }
}
conn.Close();

1 个答案:

答案 0 :(得分:0)

您不需要阅读器,循环或任何数据集。如果我理解清楚,您可以使用下面的单个查询来实现此目的。如果您需要更多帮助,请尝试告诉我。我应该工作。

INSERT INTO PartResults(PartName,
      OrderName, RequiredQuantity, StartDate, CompleteDate, 
      NormalQuantity, ScrapQuantity, OrderComment,
      SpindleTime)

  VALUES
                          Select Distinct PartName, OrderName, 
                                 RequiredQuantity, StartDate, CompleteDate, 
                                 NormalQuantity, ScrapQuantity, OrderComment,
                                 (SELECT
                                      CASE WHEN  Sum(SpindleTime) > 0 
                                     THEN            Sum(SpindleTime) 
                                      ELSE  0 END AS TotalSpindle  
                                  FROM MachineResults 
                                  WHERE OrderName =OrderResults.OrderName  ) 
                                  from OrderResults

假设OrderName是OrderResults和MachineResults之间的关系/外键.IF然后不用两个表之间的关系替换它。