首页 > 解决方案 > 将一组数据保存到一个单元格

问题描述

我想将数组保存Days到一个单元格、TimeIn_AM一个单元格、TimeOut_AM一个单元格、TimeIn_PM一个单元格和TimeOut_PM一个单元格

这是我的课程和保存方法

public class EmployeeSchedule
    {
        public string EmployeeID { get; set; }
        public string RecordID { get; set; }
        public Schedule[] Schedule { get; set; }
    }
    public class Schedule
    {
        public string Days { get; set; }
        public string TimeIn_AM { get; set; }
        public string TimeOut_AM { get; set; }
        public string TimeIn_PM { get; set; }
        public string TimeOut_PM { get; set; }
    }
 for (int i = 0; i < employeeschedule.Schedule.Length; i++)
{
  SqlCommand mycommand = new SqlCommand("WorkSchedule_Save", MyConnection);
  mycommand.CommandType = CommandType.StoredProcedure;
  mycommand.Parameters.Add("@EmployeeID", SqlDbType.VarChar).Value = employeeschedule.EmployeeID;
  mycommand.Parameters.Add("@RecordID", SqlDbType.Int).Value = employeeschedule.RecordID;
  mycommand.Parameters.Add("@Days", SqlDbType.VarChar).Value = employeeschedule.Schedule[i].Days;
  mycommand.Parameters.Add("TimeIn_AM", SqlDbType.VarChar).Value = employeeschedule.Schedule[i].TimeIn_AM;
  mycommand.Parameters.Add("TimeOut_AM", SqlDbType.VarChar).Value = employeeschedule.Schedule[i].TimeOut_AM;
  mycommand.Parameters.Add("TimeIn_PM", SqlDbType.VarChar).Value = employeeschedule.Schedule[i].TimeIn_PM;
  mycommand.Parameters.Add("TimeOut_PM", SqlDbType.VarChar).Value = employeeschedule.Schedule[i].TimeOut_PM;
}

我希望类中的所有数组都使用1EmployeeID和 1 ,但是当我保存它时,它会为数组中的每个数据创建一个和。RecordIDScheduleEmployeeIDRecordID

这是我要保存的数据

{
  "EmployeeID": "1000415",
  "RecordID": "1005",
  "Schedule": [
    {
      "Days": "Sunday",
      "TimeIn_AM": "",
      "TimeOut_AM": "",
      "TimeIn_PM": "",
      "TimeOut_PM": ""
    },
    {
      "Days": "Monday",
      "TimeIn_AM": "9:00",
      "TimeOut_AM": "12:00",
      "TimeIn_PM": "1:00",
      "TimeOut_PM": "5:00"
    },
    {
      "Days": "Tuesday",
      "TimeIn_AM": "9:00",
      "TimeOut_AM": "12:00",
      "TimeIn_PM": "1:00",
      "TimeOut_PM": "5:00"
    },
    {
      "Days": "Wednesday",
      "TimeIn_AM": "9:00",
      "TimeOut_AM": "12:00",
      "TimeIn_PM": "1:00",
      "TimeOut_PM": "5:00"
    },
    {
      "Days": "Thursday",
      "TimeIn_AM": "9:00",
      "TimeOut_AM": "12:00",
      "TimeIn_PM": "1:00",
      "TimeOut_PM": "5:00"
    },
    {
      "Days": "Friday",
      "TimeIn_AM": "9:00",
      "TimeOut_AM": "12:00",
      "TimeIn_PM": "1:00",
      "TimeOut_PM": "5:00"
    },
    {
      "Days": "Saturday",
      "TimeIn_AM": "",
      "TimeOut_AM": "",
      "TimeIn_PM": "",
      "TimeOut_PM": ""
    }
  ]
}

标签: c#

解决方案


您可以但通常不会在 SQL 中这样做。(顺便说一句,这是一个 SQL 和数据建模问题,而不是 C#)。

您将有几张桌子

 1. Employee
 2. Record
 3. ScheduleEntry

并将零件中的所有详细信息保存到这些表中。此选项实际上是唯一允许您使用 SQL 有效地处理数据的选项。

一个更糟糕的选择是有一个表,其中包含您预期的每个位的列

EmployeeNumber, RecordID, timeInMondayAM, timeOutMondayAM,timeInMondayPM, timeOutMondayPM, ... timeInAMSunday.. etc.

我当然不推荐的另一个选项是为您的值数组设置一个 Text、JSON 或 XML 的计划列,并以适当的格式将它们存储在那里。


推荐阅读