简体   繁体   中英

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

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.

  • 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 *

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. (BTW this is a SQL & data modelling question, not 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.

A significantly worse option is to have one table with columns for each bit you anticipate

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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