简体   繁体   English

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

[英]save an array of data to one cell

I would like to save the array of Days to one cell, TimeIn_AM to one cell, TimeOut_AM to one cell, TimeIn_PM to one cell, and TimeOut_PM to one cell我想将Days数组保存到一个单元格,将TimeIn_AM到一个单元格,将TimeOut_AM到一个单元格,将TimeIn_PM到一个单元格,将TimeOut_PM到一个单元格

Here is my Class and Save Method这是我的类和保存方法

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;
}

I want 1 EmployeeID and 1 RecordID for all of the array in the class Schedule but when i save it it creates a EmployeeID and RecordID for each data in the array.我想要 1 EmployeeID和 1 RecordIDSchedule中的所有数组,但是当我保存它时,它会为数组中的每个数据创建一个EmployeeIDRecordID

  • And also if i move the EmployeeID and the RecordID from the loop it says stored procedure or function [WorkSchedule_Save] has too many arguments *而且,如果我从循环中移动EmployeeIDRecordID ,它会说stored procedure or function [WorkSchedule_Save] has too many arguments *

Here is the data i want to save这是我要保存的数据

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

You could but generally you don't do it this way in SQL.你可以,但通常你不会在 SQL 中这样做。 (BTW this is a SQL & data modelling question, not C#). (顺便说一句,这是一个 SQL 和数据建模问题,而不是 C#)。

You would have several tables along the lines of你会有几张桌子沿着

 1. Employee
 2. Record
 3. ScheduleEntry

and save all the details in parts into those tables.并将所有详细信息分部分保存到这些表中。 This option is really the only one that allows you to work usefully with the data using SQL.此选项实际上是唯一一个允许您使用 SQL 有效处理数据的选项。

A significantly worse option is to have one table with columns for each bit you anticipate一个明显更糟糕的选择是为您预期的每一位设置一个带有列的表

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

Another option that I certainly wouldn't recommend is to have a schedule column of Text, JSON or XML for your array of values and store them there in an appropriate format.我当然不会推荐的另一个选项是为您的值数组设置一个文本、JSON 或 XML 的时间表列,并以适当的格式将它们存储在那里。

You can create these two tables您可以创建这两个表

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]) 
)

I think it's better to change the classes to我认为最好将课程更改为

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; }
    }

and then when you save save to EmployeeSchedule and then save to Schedule然后当您保存保存到EmployeeSchedule然后保存到Schedule

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

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