繁体   English   中英

将一组数据保存到一个单元格

[英]save an array of data to one cell

我想将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;
}

我想要 1 EmployeeID和 1 RecordIDSchedule中的所有数组,但是当我保存它时,它会为数组中的每个数据创建一个EmployeeIDRecordID

  • 而且,如果我从循环中移动EmployeeIDRecordID ,它会说stored procedure or function [WorkSchedule_Save] has too many arguments *

这是我要保存的数据

{
  "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": ""
    }
  ]
}

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

你会有几张桌子沿着

 1. Employee
 2. Record
 3. ScheduleEntry

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

一个明显更糟糕的选择是为您预期的每一位设置一个带有列的表

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

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

您可以创建这两个表

CREATE TABLE [dbo].[EmployeeSchedule]
(
    [EmployeeID ] INT NOT NULL PRIMARY KEY IDENTITY, 
    [RecordID ] INT NOT NULL
)

CREATE TABLE [dbo].[Schedule]
(
    [Days] NCHAR(50) NOT NULL, 
    [TimeIn_AM] NCHAR(50) NULL, 
    [TimeOut_AM] NCHAR(50) NULL, 
    [TimeIn_PM] NCHAR(50) NULL, 
    [TimeOut_PM] NCHAR(50) NULL, 
    [EmployeeId] INT NULL, 
    CONSTRAINT [FK_Schedule_ToEmployeeSchedule] FOREIGN KEY ([EmployeeId]) REFERENCES [EmployeeSchedule]([EmployeeID]) 
)

我认为最好将课程更改为

public class EmployeeSchedule
    {
        public string EmployeeID { get; set; }
        public string RecordID { get; set; }
        public List<Schedule> Schedules { 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; }
    }

然后当您保存保存到EmployeeSchedule然后保存到Schedule

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM